Parsing JSON Response from API(body) to receive variable

(Linus) #1

Hi All,

Im trying to read data out of my HTTP Get Response`s body however im having a hard time trying to parse the JSON to be able to work with it.

Im sending a Mobile number from user input and the following is being returned

inside this
{{make_a_http_request.body}}

.

[{"_id":“cont_004”,“name”:“Fred Adam”,“mobile_no”:“0666666666”,“office_no”:“0123454321”,“email”:“fredadam.com”}]

Then im using the Code Executer to try and parse the JSON but it isnt working.

const json = ‘{{make_a_http_request.body}}’;
const obj = JSON.parse(json);

resolve(JSON.parse(json));

Any advice?

(Nathan Stults) #2

A few comments. First, if you were to make the Content Type JSON, the output of the HTTP request would have a pre-parsed Data attribute you could index into later, removing the need for a code block at all.

However, if you want to use a code block for other reasons, a better approach is, in the code block set up, add an Input named ‘body’, set its value to {{make_a_http_request.body}}, and then do this instead:

resolve(JSON.parse(inputs.body));

Nathan

(Linus) #3

I ended up figuring it out as my JSON was in an Object.

{{getarea.data__0__olt_name}} so i added in the Object number and it worked

Thanks!