Read values from json object using keys

sreedharkaliset
Mega Expert

Hi,

I have a requirement where in i need to parse a json object without knowing the keys i get in the payload.

I found a way to get the keys using new JSON().getKeys(obj); but I could not get the values for specific keys.

I used regular parse techniques but parsing without knowing tags is not possible.

Please let me know how to achieve this

Thanks...

12 REPLIES 12

Hi Sreedhar,



Did you get answer to your question?


if yes then please mark the question as answered,


If not then you can ask your doubt here.



Regards


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Swarup Patra
Kilo Guru

Sure, you can use a for-in loop to iterate over the JSON object and get both keys and values even if you don't know the keys beforehand. Here's a sample script: javascript var jsonObject = { "key1": "value1", "key2": "value2", "key3": "value3" }; for (var key in jsonObject) { if (jsonObject.hasOwnProperty(key)) { var value = jsonObject[key]; gs.info("The key is " + key + " and the value is " + value); } } In this script: - We first define a JSON object jsonObject with some keys and values. - We then use a for-in loop to iterate over the jsonObject. - The hasOwnProperty method is used to ensure that the key we get is an actual property of jsonObject, not a property inherited from the prototype chain. - We then get the value of each key with jsonObject[key]. - Finally, we log the key and its corresponding value with gs.info. This script will log all keys and their corresponding values in the JSON object, even if you don't know what the keys are beforehand. nowKB.com

Ramesh Lohar
Kilo Guru

Sure, you can use a recursive function to parse a JSON object without knowing the keys. Here's a sample script that you can use:

javascript
function parseJSON(obj) {
for (var key in obj) {
if (typeof obj[key] === 'object' && obj[key] !== null) {
parseJSON(obj[key]);
} else {
gs.info('Key: ' + key + ', Value: ' + obj[key]);
}
}
}

var jsonString = '{"key1":"value1","key2":{"subKey1":"subValue1","subKey2":"subValue2"}}';
var jsonObject = new JSON().decode(jsonString);
parseJSON(jsonObject);


In this script:

- A function parseJSON is defined which takes an object as an argument.
- It iterates over each key in the object.
- If the value of a key is an object, it calls itself recursively to parse the sub-object.
- If the value is not an object, it logs the key and its value.

This script will work for any JSON object, regardless of the structure or the keys.

To summarize:

- Define a recursive function to parse the JSON object.
- In the function, iterate over each key in the object.
- If the value of a key is an object, call the function recursively.
- If the value is not an object, log the key and its value.
- Use the new JSON().decode() method to convert the JSON string into an object.
- Call the function with the JSON object as the argument.


nowKB.com