Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Apigee - Json.Stringify(request.content) Boolean added with quotes

I am trying to add square brackets for json. Eg., {Phone:true} to [{Phone:true}]. But in apigee I have tried multiple options using javascript. It's adding brackets but Boolean and null values added with quotes.  It is coming like [{Phone: "true"}]

Anyone faced similar problem?

 

 

 

 

 

 

 

 

0 8 2,234
8 REPLIES 8

Tried below code

var requestBody = request.content.asJSON;

    var resBody = [];
    resBody.push(requestBody);
    context.setVariable("request.content", resBody);

 

And this. 

request.content = "[" + context.targetRequest.body + "]";

 

 

Hey, thanks for the question.

  1. You haven't specified what the request content is.

    These are both valid JSON

    option 1

    { "Phone" : true }

    option 2

    { "Phone" : "true" }

    I think you are saying that you want the true value to be either quoted or not quoted. But that depends on what the inbound JSON is. Apigee won't convert a quoted string that represents a boolean value in English (eg "true"), to an actual unquoted boolean value (true). If your inbound value is like option 1, then you will get an unquoted value in output. If your inbound value is like option 2, then you will get a quoted value in output. If you want to change that behavior, then you will have to manually convert from English-language strings to boolean values in the JavaScript callout. Here's a hint for how to do it for a single property. You would need to "walk the JSON graph" to apply that conversion to all properties in the JSON hash.

    BTW, the snip you showed :

    { Phone : true } 

    ...is not valid JSON. Properties in JSON need to be surrounded in double-quotes. The bare word Phone in that snippet is not surrounded in quotes, which means that snip is not valid JSON.

  2. You want to add square brackets.

    I think simple text wrapping like this:

    request.content = "[" + something + "]";

    ...will work fine for you as long as the something is a valid json string.

Thanks for the quick response.

I am adding more details about my issue.

1. You haven't specified what the request content is.  - It is request.content ( i am trying to add brackets for the request.content) which is received from my backend application

I have tried this request.content = "[" + request.content + "]";  the result is look like below

Expected result

[{"field1":"xxxxxxx","field2":"NA","email":xxx@xxx.com,"field3":true,"utm_source":null}]

But getting like below

[{"field1":"xxxxxxx","field2":"NA","email":xxx@xxx.com,"field3":"true","utm_source":"null"}]

Boolean and null surrounded with double quotes

Note: This problem occurs only while adding brackets with request.content not for the hard coded JSON string

Please help here

I'll rephrase what I said in my earlier to emphasize it. Supposing you use this logic in your JS callout:

 

 

context.setVariable('request.content',
     "[" + context.getVariable('request.content') + "]");

 

 

This logic is merely pre-pending an open square bracket, and appending a close square bracket. This logic treats the  input as a simple string.  In this case, the output you get will be dependent on the input request.content. Apigee will not change the quoting.

Therefore, if you use the above logic, then we can expect this behavior:

  • If your inbound value (you told me it is request.content) is like this

    {"email":"xxx@xxx.com","field3":true,"utm_source":null}

    ...then the output will be like this:

    [{"email":"xxx@xxx.com","field3":true,"utm_source":null}]
  • If your inbound value surrounds values in quotes, like this

    {"email":xxx@xxx.com,"field3":"true","utm_source":"null"}

    ...then the output will be like this, WITH QUOTES.

    [{"email":"xxx@xxx.com","field3":"true","utm_source":"null"}]

The JS code isn't affecting any quotes. It's simply prepending and appending square brackets. If your input was d8 then the output would be [d8].

If you want to change that behavior, then you will have to manually convert from English-language strings to boolean values in the JavaScript callout.Here's a hint for how to do it for a single property. You would need to parse the string into a JSON object with JSON.parse(), then  "walk the JSON graph" of the resulting object to apply that conversion to all properties in the JSON hash.

Thanks again.  Agree, That is the way it should work. But not sure why the below code adding double quotes for the boolean and null fields

context.setVariable('request.content',
     "[" + context.getVariable('request.content') + "]");

 

Can I get any online meeting support from google for this issue?   Please suggest.  My Apigee trace clearly showing the converted values 😞 

I have tried multiple options but nothing helped.  Any online troubleshooting session possible?

Options tried:

1)

<Payload contentType="text/plain" variablePrefix="@" variableSuffix="#">
[@request.content#]
</Payload>

2)

context.setVariable('request.content', "[" + context.getVariable('request.content') + "]");

3)

var requestBodyContentArray = [];
requestBodyContentArray.push(requestBodyContent);

 

request.content before adding brackets:

{"field1":"xxxxxxx","field2":"NA","field3":"xxx@xxx.com","field5":null,"field6":true}

 request.content after adding brackets:

[{"field1":"xxxxxxx","field2":"NA","field3":"xxx@xxx.com","field5":"null","field6":"true"}]

I'm sorry, I don't believe you. I understand the words you are writing, and I don't believe they are true.  

I built a proxy demonstrating what I described would happen, above.  It takes the input, and wraps it in square brackets, and then sends that back in the response. 

here's a simple test illustrating how it works: 

 

$ curl -i -X POST $endpoint/square-brackets/t1 -d '{"email":"xxx@xxx.com","field3":true,"utm_source":null}'
HTTP/2 200 
content-length: 57
content-type: application/json
apiproxy: square-brackets r1
x-request-id: 8a8db449-0299-43ce-8147-385e81cb4e86
date: Tue, 11 Jan 2022 17:11:03 GMT
server: apigee
via: 1.1 google, 1.1 google
alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000

[{"email":"xxx@xxx.com","field3":true,"utm_source":null}]

 

As you can see it is not changing the quoting of the inbound payload. It simply wraps it in square brackets as I said.  

It works with either JavaScript, like this: 

<Javascript name='JS-Add-Square-Brackets'>
  <Source>
context.setVariable('modified_content',
     "[" + context.getVariable('request.content') + "]");
  </Source>
</Javascript>

Or with an AssignMessage, like this: 

<AssignMessage name='AM-Response-2'>
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
  <Set>
    <Payload contentType="text/plain">[{request.content}]</Payload>
    <ReasonPhrase>OK</ReasonPhrase>
    <StatusCode>200</StatusCode>
  </Set>
  <AssignTo>response</AssignTo>
</AssignMessage>

The results are the same.

The proxy is attached here.

Deploy it to your own environment and try it.  It will work the way I described.

If you see changes in quotes in your proxy, You're doing something in your proxy that is not quite in agreement with what you are claiming that are doing.  

I don't think the surest path to joy involves a session with Apigee support.  Of course under your support contract (if you have one) you are welcome to open a ticket. They'll not give you any information that is different than what I gave you here.  

Instead, I think you can solve this yourself. Have a closer look at your own proxy, or engage the assistance of a buddy to look closely at what you're doing in the proxy.

 

Thanks a lot for the sample proxy. This will be very helpful.  I will double check my proxy.