Extracting values from a JSON data file

You can use a Java script to extract the values from the intricate fields listed in a JSON data file. Slack, and many other applications used for exchanging data, use JSON, which is JavaScript object notation.


If you have collected a JSON data file which looks like this:


{

"Acme": {

"Department": "Finance",

"Group": "Investments",

"year": 2020,

"custodians": ["Smith", "Jones", "Wilson"],

"litigation": "Riley v. Acme",

"price": {

"1 widget": "$250", "2 widgets": "$420", "4 widgets": "$1200"

}

}

}`



. . . a Java script can help you pull out the substantive information. On Chrome, right click and select 'Inspect'. In the developer pane which opens click on the Console tab. Enter the below script which includes the text of the JSON data file:


// apps.js


let discovery = `{

"Acme": {

"Department": "Finance",

"Group": "Investments",

"year": 2020,

"custodians": ["Smith", "Jones", "Wilson"],

"litigation": "Riley v. Acme",

"price": {

"1 widget": "$250", "2 widgets": "$420", "4 widgets": "$1200"

}

}

}`;


// Converting JSON object to JS object

let obj = JSON.parse(discovery);


// Define recursive function to print nested values

function printValues(obj) {

for(let k in obj) {

if(obj[k] instanceof Object) {

printValues(obj[k]);

} else {

console.log(obj[k]);

};

}

};


// Printing all the values from the resulting object

printValues(obj);


When you press ENTER the script will generate at the end of the Console, a list of the values from the data file:



Thanks to Krunal Lathiya for posting this script here!