Access values from JSON object
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-31-2023 09:23 AM - edited 10-31-2023 10:40 AM
Hi All,
We need to access values from JSON object and we have a property which is having the data in JSON format and in the script we are parsing it. Please find below
{
"Eng": [
{
"value1": "5",
"value2": "6"
}
],
"As1": [
{
"value1": "ready",
"value2": "wip"
}
]
} this is the value in property and
var current_sys = '2a95b06c1bce3950fa3737ff034bcbc9';
var t= [];
t = JSON.parse(gs.getProperty('.'));
I need key pair values when use t.Eng[0].value1 , i am getting the value but I need to get those by using for loop.
Could anyone help on this.
Thanks in Advance!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-31-2023 09:25 AM
HI @abed123 ,
I trust you are doing great.
// Parsing the JSON property
var t = JSON.parse(gs.getProperty('x_payp2_pp_tprm.state_engagement'));
// Iterating over the main keys such as "Eng" and "As1"
for (var key in t) {
// Checking if the key is an own property of the object
if (t.hasOwnProperty(key)) {
// Accessing the array associated with each main key
var array = t[key];
// Iterating over each object in the array
for (var i = 0; i < array.length; i++) {
// Accessing each key-value pair in the object
for (var innerKey in array[i]) {
if (array[i].hasOwnProperty(innerKey)) {
// Logging the key and value
gs.info("Key: " + innerKey + ", Value: " + array[i][innerKey]);
}
}
}
}
}
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi