Unable to convert JSON object to JSON payload

ateebshaik
Participant III

Hi

I have been trying to convert the JSON object into a JSON payload using object.content.asJSON in a javascript.

But when i try to stringify the object i am still getting the same object but not the content as JSON.

I am using the below code.

var customeroutput=context.session["output"];

var output1=customeroutput.getResponse();

var jsonoutput=output1.content.asJSON;

context.setVariable("op1",JSON.stringify(jsonoutput));

Can anyone help me out whats wrong with the code .

Solved Solved
0 9 4,999
1 ACCEPTED SOLUTION

Mohammad, you wrote:

My source is in xml i want to convert that into JSON . When i use output1.content.asJSON in my code i am able to convert it into JSON object .I want to see the content not the object in my output.

I can use xml to json policy but i wanted to find another way by conveting it into JSON object and see the content in JSON format

Ah, I see.

Well, I think you have an incorrect understanding. The .asJSON notation is intended to allow you to access a text payload that is JSON, without calling JSON.parse() on the content. If the response content is actual JSON, then .asJSON is a kind of special shorthand, available only within JS callouts in Apigee Edge, that allows you to access the content that way.

The .asXML is similar, but it will work only if the response content (or more correctly the message content - it works with the request OR the response), is actually XML. If the content is XML, then the .asXML notation can let you access the elements in that XML without attempting to deserialize the XML.

These notations do not convert between XML and JSON. They are intended to provide a simpler syntax for you to use within Javascript, to access what is already XML (.asXML) or JSON (.asJSON).

So I think your understanding of the .asJSON notation was not correct.

Second thing - why would you want to NOT use the XMLToJSON policy to do the thing you want? It seems like the perfect tool for the job of converting XML to JSON.

View solution in original post

9 REPLIES 9

Not applicable

Why dont you do this:

var resJson = JSON.parse(output1);

that should give you a JSON object.

Hi Rangan,

I already have the object . I wanted to see the content in json as my source is xml

nmallesh
Participant V

Hi @Mohammad Ateeb Shaik,

Kindly tell us what exactly you want to do -

In the snippet you have provided us , you are trying to get an object and parse it to JSON and eventually converting it to string and assigning it to another variable.

Refer to the following sample snippet, for reference.

var input1=context.getVariable("request.content");  // from source (assuming to be JSON)
var output1=context.setVariable("response.content",input1); //assign directly 

The above code simply copies JSON payload from the request to response payload. You can vary the variables and pick the valid JSON and assign it to the response.

request.content and response.content are pre-defined variables in apigee

request.content

Scope begins: Proxy request
Type: String
Permission: Read/Write

Gets or sets the payload of the request message.
response.contentScope begins: Target response

Type: String
Permission: Read/Write

Payload content of the response message returned by the target


You can read more about the pre-defined set of variables available in Variable Reference

Hope this helps, keep us posted!

Hi Nisha

My source is in xml i want to convert that into JSON . When i use output1.content.asJSON in my code i am able to convert it into JSON object .I want to see the content not the object in my output.

I can use xml to json policy but i wanted to find another way by conveting it into JSON object and see the content in JSON format

Hi @Mohammad Ateeb Shaik,

Kindly provide all the necessary details, the requirement of converting XML to JSON was not mentioned earliar in the description.

Using XML-to-JSON policy is preferrable for converting XML payload to JSON payload.

Not clear what you mean by this:

i am still getting the same object but not the content as JSON.

Can you be explicit? paste screenshots or code/data snips? Tell us exactly what you are seeing, and also what you expect or desire to see.

Hello Dino,

The point i wanted to tell was . After the convertion using jsonoutput=output1.content.asJSON . Now i have the JSON object in jsonoutput variable. I wanted to see the content of this as my source is an xml and i wanted to convert that into JSON without using XML to JSON policy

Mohammad, you wrote:

My source is in xml i want to convert that into JSON . When i use output1.content.asJSON in my code i am able to convert it into JSON object .I want to see the content not the object in my output.

I can use xml to json policy but i wanted to find another way by conveting it into JSON object and see the content in JSON format

Ah, I see.

Well, I think you have an incorrect understanding. The .asJSON notation is intended to allow you to access a text payload that is JSON, without calling JSON.parse() on the content. If the response content is actual JSON, then .asJSON is a kind of special shorthand, available only within JS callouts in Apigee Edge, that allows you to access the content that way.

The .asXML is similar, but it will work only if the response content (or more correctly the message content - it works with the request OR the response), is actually XML. If the content is XML, then the .asXML notation can let you access the elements in that XML without attempting to deserialize the XML.

These notations do not convert between XML and JSON. They are intended to provide a simpler syntax for you to use within Javascript, to access what is already XML (.asXML) or JSON (.asJSON).

So I think your understanding of the .asJSON notation was not correct.

Second thing - why would you want to NOT use the XMLToJSON policy to do the thing you want? It seems like the perfect tool for the job of converting XML to JSON.

Thanks a lot 🙂 Got a clear picture about that now