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

发布于 2025-02-04 00:19:12 字数 3085 浏览 1 评论 0原文

我一直在与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从json文件中友好地做的错误,请让我知道

预期输出:

”在此处输入图像说明”

I've been messing around with JSON for some time, just pushing it out as text and it hasn't hurt anybody (that I know of), but I'd like to start doing things properly.

this is my code :

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

this is my json file:

"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"
            }
        ]
    },

my output is these:

FLETP
EA
0
5.8
20.35
18.95
14.47

I want output like in these format as you have shown in the pic if you see in the JSON file I would like to extract name and value from the JSON file kindly what I can doing mistake please let me know

Expected Output:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七禾 2025-02-11 00:19:12

做到这一点的两种方法:

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

给定数据:< /strong>

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

Two ways to do that:

  1. You can use pandas to put that into a dataframe/table (with .json_normalize()
  2. Just use the for loop

Given data:

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"
            }
        ]
    }}

Code 1:

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

Output:

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...

Code 2:

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

Output:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文