1

I have a json file where I have few data(data.json)

{
  "currency": {
    "SAR": "Saudi Riyal",
    "EUR": "Euro",
    "KWD": "Kuwaiti Dinar",
    "OMR": "Omani Rial",
    "INR": "Indian Rupee",
    "AED": "UAE Dirham",
    "EGP": "Egyptian Pound",
    "PKR": "Pakistan Rupee",
    "USD": "US Dollar",
    "LKR": "Sri Lanka Rupee",
    "BHD": "Bahraini Dinar",
    "CZK": "Czech Koruna",
    "JOD": "Jordanian Dinar",
    "KZT": "Tenge",
    "NPR": "Nepalese Rupee",
    "QAR": "Qatari Rial",
    "RUB": "Russian Ruble",
    "THB": "Baht"
  },
  "statusCode": [ "FAILED", "VOIDED", "IN_PROGRESS", "SUCCESS", "PARTIAL_REFUND", "CAPTURED", "CAPTURE_FAILED", "REFUNDED" ],
  "paymentMethods": [ "BSP", "CARD", "VCHR", "INVC" ]
}

Ive got the data with http get, but the problem is if Im stringifying I`m getting

""{\r\n  \"currency\": {\r\n    \"SAR\": \"Saudi Riyal\",\r\n    \"EUR\": \"Euro\",\r\n    \"KWD\": \"Kuwaiti Dinar\",\r\n    \"OMR\": \"Omani Rial\",\r\n    \"INR\": \"Indian Rupee\",\r\n    \"AED\": \"UAE Dirham\",\r\n    \"EGP\": \"Egyptian Pound\",\r\n    \"PKR\": \"Pakistan Rupee\",\r\n    \"USD\": \"US Dollar\",\r\n    \"LKR\": \"Sri Lanka Rupee\",\r\n    \"BHD\": \"Bahraini Dinar\",\r\n    \"CZK\": \"Czech Koruna\",\r\n    \"JOD\": \"Jordanian Dinar\",\r\n    \"KZT\": \"Tenge\",\r\n    \"NPR\": \"Nepalese Rupee\",\r\n    \"QAR\": \"Qatari Rial\",\r\n    \"RUB\": \"Russian Ruble\",\r\n    \"THB\": \"Baht\"\r\n  },\r\n  \"statusCode\": [ \"FAILED\", \"VOIDED\", \"IN_PROGRESS\", \"SUCCESS\", \"PARTIAL_REFUND\", \"CAPTURED\", \"CAPTURE_FAILED\", \"REFUNDED\" ],\r\n  \"paymentMethods\": [ \"BSP\", \"CARD\", \"VCHR\", \"INVC\" ]\r\n}\r\n\r\n""

Ive got the response in variable success, but I cant traverse. Success looks like,

"{
  "currency": {
    "SAR": "Saudi Riyal",
    "EUR": "Euro",
    "KWD": "Kuwaiti Dinar",
    "OMR": "Omani Rial",
    "INR": "Indian Rupee",
    "AED": "UAE Dirham",
    "EGP": "Egyptian Pound",
    "PKR": "Pakistan Rupee",
    "USD": "US Dollar",
    "LKR": "Sri Lanka Rupee",
    "BHD": "Bahraini Dinar",
    "CZK": "Czech Koruna",
    "JOD": "Jordanian Dinar",
    "KZT": "Tenge",
    "NPR": "Nepalese Rupee",
    "QAR": "Qatari Rial",
    "RUB": "Russian Ruble",
    "THB": "Baht"
  },
  "statusCode": [ "FAILED", "VOIDED", "IN_PROGRESS", "SUCCESS", "PARTIAL_REFUND", "CAPTURED", "CAPTURE_FAILED", "REFUNDED" ],
  "paymentMethods": [ "BSP", "CARD", "VCHR", "INVC" ]
}

"

I`ll also show my http get

 getFileContent = (fileName) => {
        let apiUrl = './assets/data/' + fileName;
        return this.http.get(apiUrl, { responseType: 'text' });
    }

this.apiService.getFileContent("data.json").subscribe(
        success => {
            console.log(success);
        }, error => {
            console.log(error);
        }
    )

Kindly help me find whats wrong, Im new to angular. My angular version is 6. Those data are created for dropdown mapping, let me know if there is a better approach

1 Answer 1

4

Found what mistake I have done, should have given responsetype: json

 getFileContent = (fileName) => {
        let apiUrl = './assets/data/' + fileName;
        return this.http.get(apiUrl, { responseType: 'json' });
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.