Reputation: 25
I'm new to javascript and need to iterate through a JSON list of dictionaries to create an list for each key.
Here's my JSON:
[
{
"Date": "Aug. 14, 2015",
"Reel": "Reel 1",
"Job": "Color Correction",
"Status": "In Progress",
"Completion": "60"
},
{
"Date": "Aug. 14, 2015",
"Reel": "Reel 1",
"Job": "Conform",
"Status": "In Progress",
"Completion": "70"
},
{
"Date": "Aug. 14, 2015",
"Reel": "Reel 2",
"Job": "Scanning",
"Status": "Complete",
"Completion": "100"
},
{
"Date": "Aug. 12, 2015",
"Reel": "Reel 1",
"Job": "QC only",
"Status": "In Progress",
"Completion": "50"
}
]
I'm using this function to read in JSON:
$.getJSON('cgi-bin/fakedata.txt', function(main)
I want to be able to access main['Date'] = ['August 12, 2015','August 14, 2015']
etc...
Upvotes: 0
Views: 4838
Reputation: 185
This should work
var x = JSON.stringify(json,["date"]);
You can then make it a JSON.
Upvotes: 0
Reputation: 522
Below is a runnable node program. It looked like to me you were trying to get the dates into a separate array. So you could access them like dates[0].
#!/usr/bin/node
json = [
{
"Date": "Aug. 14, 2015",
"Reel": "Reel 1",
"Job": "Color Correction",
"Status": "In Progress",
"Completion": "60"
},
{
"Date": "Aug. 14, 2015",
"Reel": "Reel 1",
"Job": "Conform",
"Status": "In Progress",
"Completion": "70"
},
{
"Date": "Aug. 14, 2015",
"Reel": "Reel 2",
"Job": "Scanning",
"Status": "Complete",
"Completion": "100"
},
{
"Date": "Aug. 12, 2015",
"Reel": "Reel 1",
"Job": "QC only",
"Status": "In Progress",
"Completion": "50"
}
];
function getDates(main) {
dates = [];
for (var i = 0; i < main.length; i++ ) {
dates.push(main[i].Date);
}
return dates;
};
dates = getDates(json);
console.log(dates);
Upvotes: 1
Reputation: 1373
function fieldValues(main, field) {
var res = {};
for(item of main) {
res[item[field]] = 0;
}
return Object.keys(res);
}
var main = [
{
"Date": "Aug. 14, 2015",
"Reel": "Reel 1",
"Job": "Color Correction",
"Status": "In Progress",
"Completion": "60"
},
{
"Date": "Aug. 14, 2015",
"Reel": "Reel 1",
"Job": "Conform",
"Status": "In Progress",
"Completion": "70"
},
{
"Date": "Aug. 14, 2015",
"Reel": "Reel 2",
"Job": "Scanning",
"Status": "Complete",
"Completion": "100"
},
{
"Date": "Aug. 12, 2015",
"Reel": "Reel 1",
"Job": "QC only",
"Status": "In Progress",
"Completion": "50"
}
];
console.log(fieldValues(main, "Date"));
Upvotes: 0