logo

NJP

JSON Parsing

Import · Aug 25, 2019 · article

3) Parse Values from Single Array in JSON Object :

Lets increase the complexity and now we will try to parse Value from single array embedded into an JSON object. Taking below sample example where we will try to parse number key from object.

Sample JSON:

{ "arr": [ { "parent": "", "made_sla": "true", "watch_list": "", "u_when_needed": "2017-05-18 21:52:02", "upon_reject": "cancel", "sys_updated_on": "2017-01-30 22:51:35", "u_what_needed": "legal1", "approval_history": "", "number": "NI002004", "sys_updated_by": "admin", "user_input": "", "sys_created_on": "2017-01-30 22:51:35", "state": "3" } ]

}

Code:

Var obj={"arr":[{"parent":"","made_sla":"true","watch_list":"","u_when_needed":"2017-05-18 21:52:02","upon_reject":"cancel","sys_updated_on":"2017-01-30 22:51:35","u_what_needed":"legal1","approval_history":"","number":"NI002004","sys_updated_by":"admin","user_input":"","sys_created_on":"2017-01-30 22:51:35","state":"3"}]};

var str = JSON.stringify(obj);

var parser = new JSONParser();

var parsed = parser.parse(str);

var result = parsed.arr[0].number;

gs.print(‘Number : ’+result);

Result:

*** Script:Number :NI002004

[0:00:00.001] Total Time

4) Parse Value from Nested array in JSON Object:

Taking reference of the above example now we will see how to parse attribute from nested array embedded in JSON Object.

Sample JSON:

{

"name": "Peter parker",

"heroName": "Spiderman",

"friends": [{

"heroName": "Deadpool",

"skills": ["Martial artist", "Assassin"]

}, {

"heroName": "Hulk",

"skills": ["Superhuman Speed", "Superhuman Strength"]

}, {

"heroName": "Wolverine",

"skills": ["Retractable bone claws", "Superhuman senses"]

}]

}

Code:

Var abc = {"name":"Peter parker","heroName":"Spiderman","friends":[{"heroName":"Deadpool","skills":["Martial artist","Assassin"]},{"heroName":"Hulk","skills":["Superhuman Speed","Superhuman Strength"]},{"heroName":"Wolverine","skills":["Retractable bone claws","Superhuman senses"]}]};

var a = JSON.stringify(abc);

var parser = new JSONParser();

var parsed = parser.parse(a);

gs.print(‘Length of friends array : ’+parsed.friends.length); //This will give you length of friends array which is nested array.

gs.print(‘Friends Object : ’+parsed.friends[0]);//This will give you [Object object] for 0 index on friends array

gs.print(‘Skills Object from Friends of [0] index : ’+parsed.friends[0].skills);//This will give you values in skills array of 0 Index friends array. note skills is another array

gs.print(‘Skills value of index [0] : ’+parsed.friends[0].skills[0]);//This will give Martial artist from index 0 skills array from friends 0 index array.

Result:

*** Script: Length of friends array :3 *** Script: Friends Object : [object Object] *** Script: Skills Object from Friends of [0] index : Martial artist,Assassin *** Script: ‘Skills value of index [0] : ’Martial artist

[0:00:00.004] Total Time

5) How For loop can be used to parse through array:

In this example we will see how can we use for loop to parse through values in JSON and array.

Sample JSON:

Same JSON from above example

Code:

var abc = {"name":"Peter parker","heroName":"Spiderman","friends":[{"heroName":"Deadpool","skills":["Martial artist","Assassin"]},{"heroName":"Hulk","skills":["Superhuman Speed","Superhuman Strength"]},{"heroName":"Wolverine","skills":["Retractable bone claws","Superhuman senses"]}]};

gs.print(abc);

var a = JSON.stringify(abc);

var parser = new JSONParser();

var parsed = parser.parse(a);

var str = '';

var str1 = '';

for (var i in abc.friends) {

str += abc.friends[i].heroName + "
";

for (var j in abc.friends[i].skills) {

str1 += abc.friends[i].skills[j] + "
";

}

}

gs.print(str);

gs.print(str1);

Result:

2019-08-25T11:56:29.163Z: [object Object]
2019-08-25T11:56:29.163Z: Deadpool Hulk Wolverine
2019-08-25T11:56:29.163Z: Martial artist Assassin Superhuman Speed Superhuman Strength Retractable bone claws Superhuman senses

Friends, this is very generic blog and will be useful in day to day coding for a learner or intermediate developer. Trying to help with this and will come up with new case's as i encountered them in practical implementation.

If you guys have any comments/ suggestions please leave it here and i will work on that. Bookmark this if you find it helpful.

Thanks,
Ashutosh Munot

MVP 2019

Labels:

View original source

https://www.servicenow.com/community/developer-blog/json-parsing/ba-p/2279166