2013-10-26 119 views
0

我有这个数据库http://ashleyw.co.uk/project/food-nutrient-database,我想将组Dairy and Egg Products提取到一个单独的Json文件中。如何从JSON文件中提取特定数据

另外,在组内部分数据是三重的。例如:

{ 
    "id": 1008, 
    "description": "Cheese, caraway", 
    "tags": [], 
    "manufacturer": "", 
    "group": "Dairy and Egg Products", 
    "portions": [ 
     { 
      "amount": 1, 
      "unit": "oz", 
      "grams": 28.35 
     } 
    ], 
    "nutrients": [ 
     { 
      "value": 25.18, 
      "units": "g", 
      "description": "Protein", 
      "group": "Composition" 
     }, 
     { 
      "value": 29.2, 
      "units": "g", 
      "description": "Total lipid (fat)", 
      "group": "Composition" 
     }, 
     { 
      "value": 3.06, 
      "units": "g", 
      "description": "Carbohydrate, by difference", 
      "group": "Composition" 
     }, 
     { 
      "value": 25.18, 
      "units": "g", 
      "description": "Protein", 
      "group": "Composition" 
     }, 
     { 
      "value": 29.2, 
      "units": "g", 
      "description": "Total lipid (fat)", 
      "group": "Composition" 
     }, 
     { 
      "value": 3.06, 
      "units": "g", 
      "description": "Carbohydrate, by difference", 
      "group": "Composition" 
     }, 
     { 
      "value": 25.18, 
      "units": "g", 
      "description": "Protein", 
      "group": "Composition" 
     }, 
     { 
      "value": 29.2, 
      "units": "g", 
      "description": "Total lipid (fat)", 
      "group": "Composition" 
     }, 
     { 
      "value": 3.06, 
      "units": "g", 
      "description": "Carbohydrate, by difference", 
      "group": "Composition" 
     } 
    ] 
} 

nutrients节点的每个孩子都重复三次。怎样才能剥离临时演员?

+0

也许尝试[JQ](http://stedolan.github.io/jq/)如果你想有一个CLI工具:) – gustavohenke

+0

@LutzHorn我最熟悉JavaScript。尽管我对编程一般都很陌生。你能解释你的代码吗? – Yansee

回答

0

使用Python

import json 

with open("data.json") as f, open("nutrients.json", "w") as g: 
    d = json.loads(f.read()) 
    json.dump(d["nutrients"], g)