Incorrectly formatted json array response

adamsanchez
Participant II

I have an endpoint at the moment that is returning within the response invalid formatted json as follows (which needs to be fixed in the backend but whilst thats being done would like Apigee to display this correctly in the meantime) :

"flag": "
{\"code\":\"FRA\",\"name\":\"France\",\"iso_3166_1_alpha_2\":\"FR\",\"alt_name\":\"France (Fis)\"}

and would like to have it as follows :

"flag": 
{
                    "alt_name": null,
                    "code": "BMU",
                    "iso_3166_1_alpha_2": "BM",
                    "name": "Bermuda"
                } 

Should i be using xml to json policy in respect to treating the array response or should i be using a jscript policy instead or both actually? Not getting the response with neither at the moment

Solved Solved
0 8 619
1 ACCEPTED SOLUTION

@Sanchez

Why would you be using xml to json on a not-well-formatted json?

Your samples differ in values, so I am not very clear what you want to do.

If you need to just replace the backslash escape character used for the quotes, just use javascript policy and use the replace function to remove the \ character.

View solution in original post

8 REPLIES 8

@Sanchez

Why would you be using xml to json on a not-well-formatted json?

Your samples differ in values, so I am not very clear what you want to do.

If you need to just replace the backslash escape character used for the quotes, just use javascript policy and use the replace function to remove the \ character.

Hi Team, I have context.getvariable(responsename), but seeing response with \n before and after every attribute in json array. I will need help with replacing "\n" and print formatted json.

Can you ask a new question please? and in the new question, post an example of the content you're seeing, and the content you expect to see.

This is small snippet :

{
"content": "[\n {\n \"gmb_code\": INDINDIND\",\n \"reputation_name\": \"Some Hospitalname\"]"
}

But I need it to be without \ and \n 

This is small snippet :

{
"content": "[\n {\n \"gmb_code\": INDINDIND\",\n \"reputation_name\": \"Some Hospitalname\"]"
}

But I need it to be without \ and \n  like below

{

 "gmb_code" : "INDINDIND",

"reputation_name": "Some Hospitalname"
}

can you ask a new question please?  The button is in the upper right corner.  It's labeled "Ask a Question". 

Here is js file :

var retreivedStorageFile = context.getVariable("storage.file.retrieved");

context.setVariable(retreivedStorageFile, JSON.stringify(retreivedStorageFile));

print("retreivedStorageFile is: " + context.getVariable(retreivedStorageFile));

 

Need to replace \n  and \ before and after json attribute to format json to display only formatted response form apigee proxy.