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 -
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:
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:
Why don't you expose the REST API using OutSystems?
https://success.outsystems.com/Documentation/10/Extensibility_and_Integration/REST/Expose_REST_APIs/Expose_a_REST_API
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
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:
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.
In order to get the HTTP status Code in the Rest API response, we are required to provide the handler for OnAfterResponse or OnAfterResponseAdvanced. Here in the body of the handler, we are required to update the CustomizedResponseText.
We are required to set the below properties here:
- CustomizedResponse.ResponseText
- CustomizedResponse.StatusCode
- CustomizedResponse.StatusLine
- CustomizedResponse.Headers
- CustomizedResponse.ResponseBinary
In order to get the status code, we need to pass the custom json as mentioned below:
"{" + """StatusCode""" + ":" + Response.StatusCode + ", " + """RecordsList""" + ":" + Response.ResponseText + "}"
Here Response.ResponseText is the actual response which we get from the Rest API.
In order to parse this JSON, we need to create a custom Response Structure accordingly and we need to update the datatype for the API Response property.