JSON: Parse and append

JSON: Parse and append

  

Hi,


I have a JSON ouput from a Rest API which is something like below:


{  
   "name":"abc"
}


Now I need to append another element in the above JSON so that final JSON looks something like below:


{  
    "name":"abc",

     "addr":"efg"
}


Can you please suggest how to parse a JSON and then append an element to it.


Thanks,

Hello Biplap

If you want to add this to the answer BEFORE it is translate to the OutSystems list structure, you can use OnAfterResponse and than add to the raw JSON string.

Cheers.

Hi Eduardo,

I was trying to construct the JSON by using built-infunctions - substring and length like below:

ElementOne = Response.ResposeText

ElementOneLength = Length(ElementOne) // this is step 2

FinalJSON = "{""+Substr(ElementOne,2,Length(ElementOne)) +"",""StatusCode"":""" + CustomizedResponse.StatusCode + """}"


Note Response.ResposeText = "{""name"":""ABC DE 12345""}"


So that the final JSON should look like below:

"{""name"":""ABC DE 12345"",""StatusCode"":""200""}"


But I'm getting the below:

"{""name"":""ABC DE 12345""}"",""StatusCode"":""200""}"


I found that the length function used earlier in step 2 is not giving correct result. Its giving length as 23. Because of this my final JSON is getting constructed as - 

"{""name"":""ABC DE 12345""}"",""StatusCode"":""200""}"


Can you please advise what I'm missing here.


Many thanks.



Are you Exporting a REST service in your application?
Why are you building the JSON by hand??? :o

Eduardo Jauch wrote:

Are you Exporting a REST service in your application?
Why are you building the JSON by hand??? :o

I got the response as below:

Response.ResposeText = "{""name"":""ABC DE 12345""}"

Need to append -  ""StatusCode"":""200"" in the above JSON so that the final JSON should be like: 

"{""name"":""ABC DE 12345"",""StatusCode"":""200""}"






I still puzzled why don't you simply use the default expose REST api methods in OutSystems.

You create your method, and you define your output, a structure or a list of structures, and you can even add an attribute like this one StatusCode if you need it in the JSON structure, and that's it.

No need to build it by hand... 

Like here: https://www.youtube.com/watch?v=DiKHR7Qe1Es

Cheers.

Eduardo Jauch wrote:

I still puzzled why don't you simply use the default expose REST api methods in OutSystems.

You create your method, and you define your output, a structure or a list of structures, and you can even add an attribute like this one StatusCode if you need it in the JSON structure, and that's it.

No need to build it by hand... 

Like here: https://www.youtube.com/watch?v=DiKHR7Qe1Es

Cheers.

Hi,

Actually in my application, I have consumed an external Rest API where in OnAfterResponse I can see the Response.ResposeText = "{""name"":""ABC DE 12345""}"

Now I need to customized the ResponseText so that Customized.ResponseText is "{""name"":""ABC DE 12345"",""StatusCode"":""200""}"

I tried with the built-in functions to construct the output, but not got the desired result as explained in my above posts.


Biplab Pal wrote:

Eduardo Jauch wrote:

I still puzzled why don't you simply use the default expose REST api methods in OutSystems.

You create your method, and you define your output, a structure or a list of structures, and you can even add an attribute like this one StatusCode if you need it in the JSON structure, and that's it.

No need to build it by hand... 

Like here: https://www.youtube.com/watch?v=DiKHR7Qe1Es

Cheers.

Hi,

Actually in my application, I have consumed an external Rest API where in OnAfterResponse I can see the Response.ResposeText = "{""name"":""ABC DE 12345""}"

Now I need to customized the ResponseText so that Customized.ResponseText is "{""name"":""ABC DE 12345"",""StatusCode"":""200""}"

I tried with the built-in functions to construct the output, but not got the desired result as explained in my above posts.


Why do you need to put the status response in the answer?

You can change the status response in the output structure of this action, that has an attribute for this.

If you need, for some reason, to insert inside the answer and is not being able to do it directly into the text, which, on your simple example, wpuld be to replace the last } by the required string, starting from the comma, you can deserialize to a structure with that attribute, add the value, serialize back.

OutSystems has logical statements to do this JSONSerialize and so on.

Do you really need to add this to the raw answer? Setting this value in the Customized is not enough?

Cheers


first, please validate your JSON data using these tools.


  1. https://jsonformatter.org/json-parser
  2. https://codebeautify.org/jsonvalidator