- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2025 12:23 PM - edited 03-10-2025 04:06 PM
Hi All,
Kindly help to deal below situation.
now kindly help me how to deal below format if there is nested values and push into Object {}
var j={
info:{
data: [
{
rollNumber:"98087",
name:"ABC",
section:"A"
}, {
rollNumber:"98097",
name:"ABCD",
section:"B"
}
]
}
}
Script:
system Property : List of student fields
values: rollNumber,name, section,
var gr = gs.getProperty("list of student fields").split(',');
var parse = JSON.parse(j);
var result = parse.info.data;
var obj={};
var final=[];
for(var i in gr){
if(gr[i] in result){
obj[gr[i]] = result[gr[i]].toString();
}
}
final.push(obj);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2025 04:46 PM - edited 03-10-2025 04:47 PM
Can't reproduce, check your input data maybe. I've used your data as you've described and it works fine (eg: run this in a BG script):
var fields = "rollNumber, name, section".split(/,\s*/);
var j = { info:{ data: [ { rollNumber:"98087", name:"ABC", section:"A" }, { rollNumber:"98097", name:"ABCD", section:"B" } ] } };
var data = j.info.data;
var res = [];
data.forEach(function(obj) {
var resObj = {};
fields.forEach(function(field) {
if (field in obj)
resObj[field] = obj[field];
});
res.push(resObj);
});
gs.info(JSON.stringify(res, null, 2));
/* Output:
[
{
"rollNumber": "98087",
"name": "ABC",
"section": "A"
},
{
"rollNumber": "98097",
"name": "ABCD",
"section": "B"
}
]
*/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2025 03:37 PM
Please explain what you're trying to do in detail... What is your expected output, what is "list of student fields", etc.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2025 04:07 PM - edited 03-10-2025 04:15 PM
I don't want to define field name inside script, for that I defined required fields in systemProperty and that fields checking in JSON data. if fields data available in JSON then push that data into Object.
system Property : List of student fields
values: rollNumber, name, section,
We want to push nested JOSN values into Obj
expecting output :
[{"rollNumber":98087,"name":"ABC","section":"A"},{"rollNumber":98097,"name":"ABCD","section":"B"}]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2025 04:09 PM
what's your expected output?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2025 04:15 PM
expecting output :
[{"rollNumber":98087,"name":"ABC","section":"A"},{"rollNumber":98097,"name":"ABCD","section":"B"}]