以这些格式从 json 中提取数据

Extract data from json in these format

我已经弄乱了 JSON 有一段时间了,只是将它作为文本发布并且它没有伤害任何人(据我所知),但我想开始做事正确。

这是我的代码:

term=temp['specifications']['PKG&HAZMAT']
    for j in term:
        try:
            got=j['value']
        except:
            pass
    
        print(got)

这是我的 json 文件:

"specifications": {
        
    
    "PKG&HAZMAT": [{
                "value": "FLETP",
                "name": "VMRS",
                "key": "a8f1W000000fxho"
            },
            {
                "value": "EA",
                "name": "Sales Unit",
                "key": "a8f1W000000fxhv"
            },
            {
                "value": "0",
                "name": "Quantity per Application",
                "key": "a8f1W000000fxhy"
            },
            {
                "value": "5.8",
                "name": "Height Each",
                "key": "a8f1W000000fxi2"
            },
            {
                "value": "20.35",
                "name": "Width Each",
                "key": "a8f1W000000fxi3"
            },
            {
                "value": "18.95",
                "name": "Length Each",
                "key": "a8f1W000000fxi4"
            },
            {
                "value": "14.47",
                "name": "Weight Each",
                "key": "a8f1W000000fxi5"
            },
            {
                "value": "WARNING Cancer and Reproductive Harm - www.P65Warnings.ca.gov",
                "name": "Prop 65 Statement",
                "key": "a8f1W000000g3EN"
            }
        ],
        "MARKETING": [{
                "value": "Spiral wound",
                "name": "Benefit 1",
                "key": "a8f1W000000TOAF"
            },
            {
                "value": "Includes hang collar",
                "name": "Benefit 2",
                "key": "a8f1W000000TOAG"
            },
            {
                "value": "One bundle for easy management",
                "name": "Benefit 3",
                "key": "a8f1W000000TOAH"
            }
        ],
        "PROP65": [{
                "value": "1GENERAL",
                "name": "Code",
                "key": "a8f6S000000btYS"
            },
            {
                "value": "WARNING: Cancer and Reproductive Harm - www.P65Warnings.ca.gov.",
                "name": "Short Warning",
                "key": "a8f6S000000btYT"
            }
        ],
        "FP_PartType_F552": [{
                "value": "15",
                "name": "Length",
                "key": "a8f6S000000Ynnr"
            },
            {
                "value": "ABS with zinc die cast plugs",
                "name": "Electric Cable Type",
                "key": "a8f6S000000YnYr"
            }
        ]
    },

我的输出是这些:

FLETP
EA
0
5.8
20.35
18.95
14.47

如果你在 JSON 文件中看到我想从 [=] 中提取 namevalue 31=] 请提交我可能做错了什么请告诉我

预期输出:

两种方法:

  1. 您可以使用 pandas 将其放入 dataframe/table(使用 .json_normalize()
  2. 只需使用 for 循环

给出的数据:

data = {"specifications": 
        { 
    "PKG&HAZMAT": [{
                "value": "FLETP",
                "name": "VMRS",
                "key": "a8f1W000000fxho"
            },
            {
                "value": "EA",
                "name": "Sales Unit",
                "key": "a8f1W000000fxhv"
            },
            {
                "value": "0",
                "name": "Quantity per Application",
                "key": "a8f1W000000fxhy"
            },
            {
                "value": "5.8",
                "name": "Height Each",
                "key": "a8f1W000000fxi2"
            },
            {
                "value": "20.35",
                "name": "Width Each",
                "key": "a8f1W000000fxi3"
            },
            {
                "value": "18.95",
                "name": "Length Each",
                "key": "a8f1W000000fxi4"
            },
            {
                "value": "14.47",
                "name": "Weight Each",
                "key": "a8f1W000000fxi5"
            },
            {
                "value": "WARNING Cancer and Reproductive Harm - www.P65Warnings.ca.gov",
                "name": "Prop 65 Statement",
                "key": "a8f1W000000g3EN"
            }
        ],
        "MARKETING": [{
                "value": "Spiral wound",
                "name": "Benefit 1",
                "key": "a8f1W000000TOAF"
            },
            {
                "value": "Includes hang collar",
                "name": "Benefit 2",
                "key": "a8f1W000000TOAG"
            },
            {
                "value": "One bundle for easy management",
                "name": "Benefit 3",
                "key": "a8f1W000000TOAH"
            }
        ],
        "PROP65": [{
                "value": "1GENERAL",
                "name": "Code",
                "key": "a8f6S000000btYS"
            },
            {
                "value": "WARNING: Cancer and Reproductive Harm - www.P65Warnings.ca.gov.",
                "name": "Short Warning",
                "key": "a8f6S000000btYT"
            }
        ],
        "FP_PartType_F552": [{
                "value": "15",
                "name": "Length",
                "key": "a8f6S000000Ynnr"
            },
            {
                "value": "ABS with zinc die cast plugs",
                "name": "Electric Cable Type",
                "key": "a8f6S000000YnYr"
            }
        ]
    }}

代码 1:

import pandas as pd
    
term = data['specifications']['PKG&HAZMAT']
df = pd.json_normalize(term)[['name','value']]

输出:

print(df)
                       name                                              value
0                      VMRS                                              FLETP
1                Sales Unit                                                 EA
2  Quantity per Application                                                  0
3               Height Each                                                5.8
4                Width Each                                              20.35
5               Length Each                                              18.95
6               Weight Each                                              14.47
7         Prop 65 Statement  WARNING Cancer and Reproductive Harm - www.P65...

代码 2:

term = data['specifications']['PKG&HAZMAT']
for j in term:
    name = j['name']
    value = j['value']
    
    print(name, value)

输出:

VMRS FLETP
Sales Unit EA
Quantity per Application 0
Height Each 5.8
Width Each 20.35
Length Each 18.95
Weight Each 14.47
Prop 65 Statement WARNING Cancer and Reproductive Harm - www.P65Warnings.ca.gov