使用 Python 操作大型 JSON



我有一个相当大的.json形式的数据文件,我想操作它,以下形式,就像许多json对象一样:

[
{ 
"_id" : "...", 
"idSession" : "...", 
"createdAt" : "1526894989268", 
"status" : "COMPLETE", 
"raw" : "Bobsguide,Marketing Assistant,Sales / Marketing79642,Baitshepi,,etc", 
"updatedAt" : "...", 
"graphResults" : [
[
"lastName", 
"stock"
], 
[
"country", 
"Botswana"
], 
[
"location", 
"Botswana  "
], 
[
"city", 
"-"
], 
[
"state", 
"-"
], 
[
"school", 
"Heriot-Watt University"
], 
[
"skills", 
"Budgeting,Business Process Improvement,Business Planning"
], 
], 
"eid" : {
"###" : "12020653-1889-35be-8009-b1c9d43768ac"
}
}
{ 
"_id" : "...", 
"idSession" : "...", 
"createdAt" : "1526894989268", 
"status" : "COMPLETE", 
"raw" : "Bobsguide,79619,Steven,example,steven.jones@example.com,Marketing Assistant,Sales,,etc", 
"updatedAt" : "...", 
"graphResults" : [
[
"country", 
"United Kingdom"
], 
[
"location", 
"United Kingdom London London"
], 
[
"city", 
"London"
], 
[
"state", 
"London"
], 
[
"skills", 
"Solvency II,Liquidity Risk,Screening,etc"
]
], 
"eid" : {
"###" : "..."
}
}
...

]

我有没有一种直接的方法可以将其读取到 python 脚本中进行操作/分析。感兴趣的主要部分是图形结果和原始结果的标签。我对这种形式的原始数据缺乏经验,因此非常感谢任何帮助。

首先,您发布的数据不正确,它应该如下所示,要访问您提到的元素,您可以尝试以下操作

{
"test":[
{ 
"_id" : "...", 
"idSession" : "...", 
"createdAt" : "1526894989268", 
"status" : "COMPLETE", 
"raw" : "Bobsguide,Marketing Assistant,Sales /             Marketing79642,Baitshepi,,etc", 
"updatedAt" : "...", 
"graphResults" : [
[
"lastName", 
"stock"
], 
[
"country", 
"Botswana"
], 
[
"location", 
"Botswana  "
], 
[
"city", 
"-"
], 
[
"state", 
"-"
], 
[
"school", 
"Heriot-Watt University"
], 
[
"skills", 
"Budgeting,Business Process Improvement,Business Planning"
]
], 
"eid" : {
"###" : "12020653-1889-35be-8009-b1c9d43768ac"
}
},
{ 
"_id" : "...", 
"idSession" : "...", 
"createdAt" : "1526894989268", 
"status" : "COMPLETE", 
"raw" : "Bobsguide,79619,Steven,example,steven.jones@example.com,Marketing     Assistant,Sales,,etc", 
"updatedAt" : "...", 
"graphResults" : [
[
"country", 
"United Kingdom"
], 
[
"location", 
"United Kingdom London London"
], 
[
"city", 
"London"
], 
[
"state", 
"London"
], 
[
"skills", 
"Solvency II,Liquidity Risk,Screening,etc"
]
], 
"eid" : {
"###" : "..."
}
}
]
}

//答

import json
data_file = open('data.json', 'r')
information = json.load(data_file) // this will give you a json obj
print(information['test'][1]['raw']) // would pick element 1 from array then 

在原始键中选取和打印值

print(information['test'][1]['graphResults']) // would pick element 1 from array then pick and print value in raw key

最新更新