1

My json output from service now is

[{"errno": "0","num_keys": "0"},
 {"errno": "1","num_keys": "2"},
 {"errno": "3","num_keys": "4"},
 {"errno": "5","num_keys": "6"}]

In this, i want to extract each value if keys errno and num_keys.

I am using the below code:

var request = new sn_ws.RESTMessageV2();
request.setEndpoint("url");
request.setHttpMethod('GET');
request.setRequestHeader('Content-Type','application/json');
request.setRequestHeader('X-IPM-Username','some name');
request.setRequestHeader('X-IPM-Password','some password');
var response = request.execute();
var result=response.getBody();

Here how to extract the values?

var result=response.json();
6
  • 1
    Please, please do not send passwords in a GET request. HTTP verb semantics be @#%$ed. Commented Nov 29, 2019 at 15:05
  • @JaredSmith ,thanks. here i use encoded password only, just to show code i pasted it like this Commented Nov 29, 2019 at 15:07
  • 2
    I'm not sure why the request is relevant here. If you have some JSON, and want to extract some data, it doesn't matter where that JSON came from. Commented Nov 29, 2019 at 15:07
  • 1
    @Jared If it's an API that requires authentication, and it's not a login action (i.e. something where you get a token for the right username + password), then there's absolutely nothing wrong with this. Arguably the API should use the Authorization header instead of X-..., but whatever… Commented Nov 29, 2019 at 15:08
  • I tried it and you're response body example has extra comma at the end which breaks the array: [ a, b, ] Commented Nov 29, 2019 at 15:26

2 Answers 2

1

This has likely been asked previously, but your question itself has the answer in it. You'll want to use JSON.parse() parse your JSON string into a JavaScript object, perhaps like this:

var result = JSON.parse(response.json());

Your resulting object, from your example response, would be an array of objects which you can access or iterate through:

var error0 = result[0];

Note that you may run into errors if your HTTP request fails or doesn't return properly formatted JSON.

Sign up to request clarification or add additional context in comments.

3 Comments

i tried this parse method, it gave result as**** Script: [object Object],[object Object],[object Object],[object Object] when i printed it using var result=JSON.parse(response.getBody()); gs.print(result);
@impika Sounds like it parsed successfully. Now you just need to loop through the array and do whatever it is you need to do with those objects.
@impika You are on the correct path! This is because the resulting object is an array of objects, you must iterate through the array to access the data. w3schools.com/js/js_arrays.asp
0

Simple parse :

var json = '{"myjsons":[{"errno": "0","num_keys": "0"},{"errno": "2","num_keys": "3"}]}';


var jsonData = JSON.parse(json);
for (var i = 0; i < jsonData.myjsons.length; i++) {
    var myjsons= jsonData.myjsons[i];
    console.log(myjsons.errno);
     console.log(myjsons.num_keys);
}

Also like this :

var arr =  [{"errno": "0","num_keys": "0"},
 {"errno": "1","num_keys": "2"},
 {"errno": "3","num_keys": "4"},
 {"errno": "5","num_keys": "6"}];

arr.forEach(function(value){
  console.log('errno: ' + value.errno);
  console.log('num_keys: ' + value.num_keys);

});

7 Comments

It may be applicable if one block of key value pair, however in my case it is 4 blocks of values
@impika look my update
what is here "myjsons" in jsonData.myjsons.length? apologies that i am a beginner in javascript
@impika Update your post with the full-json
it is same as what i posted, it doesnt have any variable/key assigned. I hit URL with get request, the output is what i posted
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.