top of page

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!

Sean O'Shea has more than 20 years of experience in the litigation support field with major law firms in New York and San Francisco.   He is an ACEDS Certified eDiscovery Specialist and a Relativity Certified Administrator.

The views expressed in this blog are those of the owner and do not reflect the views or opinions of the owner’s employer.

If you have a question or comment about this blog, please make a submission using the form to the right. 

Your details were sent successfully!

© 2015 by Sean O'Shea . Proudly created with Wix.com

bottom of page