How to get a particular value from JSON response ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2022 10:28 PM
Hii.. I am sharing one response here. In which I want to get the value of some fields which I need to store in custom table.
1) How to get the value of id,name and description ?
2) How to store it in custom table ?
Can anyone help me with it ? Please
var arr = '{"data":{"policyConnection":{"nodes":[{"id":"abc8c043-234-43ef-a3e2-73e94b5d3900","name":"ACPL","description":"Ambuja Cement Private Limited ","creator":null,"totalObjects":11,"numAnalyzers":3}]}}}';
- Labels:
-
Knowledge Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2022 10:43 PM
Hi Shubham,
try this on high level
var arr = '{"data":{"policyConnection":{"nodes":[{"id":"abc8c043-234-43ef-a3e2-73e94b5d3900","name":"ACPL","description":"Ambuja Cement Private Limited ","creator":null,"totalObjects":11,"numAnalyzers":3}]}}}';
var parser = JSON.parse(arr);
for(var i=0;i<parser.data.policyConnection.nodes.length;i++){
var id = parser.data.policyConnection.nodes[i].id;
var name = parser.data.policyConnection.nodes[i].name;
var description = parser.data.policyConnection.nodes[i].description;
// use GlideRecord here now to insert
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-26-2022 01:45 AM
Thank you for marking my response as helpful.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2022 10:47 PM
Hi,
Try below code
var arr = '{"data":{"policyConnection":{"nodes":[{"id":"abc8c043-234-43ef-a3e2-73e94b5d3900","name":"ACPL","description":"Ambuja Cement Private Limited ","creator":null,"totalObjects":11,"numAnalyzers":3}]}}}';
var a = JSON.parse(arr);
for (i = 0; i < a.data.policyConnection.nodes.length; i++) {
var id = a.data.policyConnection.nodes[i].id;
var name = a.data.policyConnection.nodes[i].name;
var description = a.data.policyConnection.nodes[i].description;
}
Please mark my response as correct answer or helpful if applicable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2022 11:06 PM
Hi
Use the below code, it's tested.
var arr = '{"data":{"policyConnection":{"nodes":[{"id":"abc8c043-234-43ef-a3e2-73e94b5d3900","name":"ACPL","description":"Ambuja Cement Private Limited ","creator":null,"totalObjects":11,"numAnalyzers":3}]}}}';
var data = JSON.parse(arr);
for (var i in data.data.policyConnection.nodes) {
var id = data.data.policyConnection.nodes[i].id;
var name = data.data.policyConnection.nodes[i].name;
var description = data.data.policyConnection.nodes[i].description;
var gr = new GlideRecord(<table_name>);
gr.addQuery("<query>");
gr.query();
if(gr.next()){
// do update the record
}
}
Regards,
Snehangshu Sarkar
Please mark my answer as correct if it resolves your query.