0

我有这个数据库http://ashleyw.co.uk/project/food-nutrient-database,我想将该组提取Dairy and Egg Products到一个单独的 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节点都是三重的。多余的东西怎么能去掉?

4

1 回答 1

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)
于 2013-10-26T13:46:42.510 回答