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();
impika
  • 107
  • 3
  • 11
  • 1
    Please, please do not send **passwords** in a GET request. HTTP verb semantics be @#%$ed. – Jared Smith Nov 29 '19 at 15:05
  • @JaredSmith ,thanks. here i use encoded password only, just to show code i pasted it like this – impika Nov 29 '19 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. – IMSoP Nov 29 '19 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… – deceze Nov 29 '19 at 15:08
  • I tried it and you're response body example has extra comma at the end which breaks the array: [ a, b, ] – imiro Nov 29 '19 at 15:26
  • @imiro sorry, thats by mistake and i removed it – impika Nov 29 '19 at 15:27

2 Answers2

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.

rbonestell
  • 420
  • 4
  • 13
  • 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 Nov 29 '19 at 15:15
  • @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. – deceze Nov 29 '19 at 15:16
  • @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. https://www.w3schools.com/js/js_arrays.asp – rbonestell Nov 29 '19 at 15:16
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);

});
DEVLOGIN
  • 87
  • 1
  • 9