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 :
JavaScript
x
9
1
term=temp['specifications']['PKG&HAZMAT']
2
for j in term:
3
try:
4
got=j['value']
5
except:
6
pass
7
8
print(got)
9
this is my json file:
JavaScript
1
84
84
1
"specifications": {
2
3
4
"PKG&HAZMAT": [{
5
"value": "FLETP",
6
"name": "VMRS",
7
"key": "a8f1W000000fxho"
8
},
9
{
10
"value": "EA",
11
"name": "Sales Unit",
12
"key": "a8f1W000000fxhv"
13
},
14
{
15
"value": "0",
16
"name": "Quantity per Application",
17
"key": "a8f1W000000fxhy"
18
},
19
{
20
"value": "5.8",
21
"name": "Height Each",
22
"key": "a8f1W000000fxi2"
23
},
24
{
25
"value": "20.35",
26
"name": "Width Each",
27
"key": "a8f1W000000fxi3"
28
},
29
{
30
"value": "18.95",
31
"name": "Length Each",
32
"key": "a8f1W000000fxi4"
33
},
34
{
35
"value": "14.47",
36
"name": "Weight Each",
37
"key": "a8f1W000000fxi5"
38
},
39
{
40
"value": "WARNING Cancer and Reproductive Harm - www.P65Warnings.ca.gov",
41
"name": "Prop 65 Statement",
42
"key": "a8f1W000000g3EN"
43
}
44
],
45
"MARKETING": [{
46
"value": "Spiral wound",
47
"name": "Benefit 1",
48
"key": "a8f1W000000TOAF"
49
},
50
{
51
"value": "Includes hang collar",
52
"name": "Benefit 2",
53
"key": "a8f1W000000TOAG"
54
},
55
{
56
"value": "One bundle for easy management",
57
"name": "Benefit 3",
58
"key": "a8f1W000000TOAH"
59
}
60
],
61
"PROP65": [{
62
"value": "1GENERAL",
63
"name": "Code",
64
"key": "a8f6S000000btYS"
65
},
66
{
67
"value": "WARNING: Cancer and Reproductive Harm - www.P65Warnings.ca.gov.",
68
"name": "Short Warning",
69
"key": "a8f6S000000btYT"
70
}
71
],
72
"FP_PartType_F552": [{
73
"value": "15",
74
"name": "Length",
75
"key": "a8f6S000000Ynnr"
76
},
77
{
78
"value": "ABS with zinc die cast plugs",
79
"name": "Electric Cable Type",
80
"key": "a8f6S000000YnYr"
81
}
82
]
83
},
84
my output is these:
JavaScript
1
8
1
FLETP
2
EA
3
0
4
5.8
5
20.35
6
18.95
7
14.47
8
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:
Advertisement
Answer
Two ways to do that:
- You can use
pandas
to put that into a dataframe/table (with.json_normalize()
- Just use the for loop
Given data:
JavaScript
1
83
83
1
data = {"specifications":
2
{
3
"PKG&HAZMAT": [{
4
"value": "FLETP",
5
"name": "VMRS",
6
"key": "a8f1W000000fxho"
7
},
8
{
9
"value": "EA",
10
"name": "Sales Unit",
11
"key": "a8f1W000000fxhv"
12
},
13
{
14
"value": "0",
15
"name": "Quantity per Application",
16
"key": "a8f1W000000fxhy"
17
},
18
{
19
"value": "5.8",
20
"name": "Height Each",
21
"key": "a8f1W000000fxi2"
22
},
23
{
24
"value": "20.35",
25
"name": "Width Each",
26
"key": "a8f1W000000fxi3"
27
},
28
{
29
"value": "18.95",
30
"name": "Length Each",
31
"key": "a8f1W000000fxi4"
32
},
33
{
34
"value": "14.47",
35
"name": "Weight Each",
36
"key": "a8f1W000000fxi5"
37
},
38
{
39
"value": "WARNING Cancer and Reproductive Harm - www.P65Warnings.ca.gov",
40
"name": "Prop 65 Statement",
41
"key": "a8f1W000000g3EN"
42
}
43
],
44
"MARKETING": [{
45
"value": "Spiral wound",
46
"name": "Benefit 1",
47
"key": "a8f1W000000TOAF"
48
},
49
{
50
"value": "Includes hang collar",
51
"name": "Benefit 2",
52
"key": "a8f1W000000TOAG"
53
},
54
{
55
"value": "One bundle for easy management",
56
"name": "Benefit 3",
57
"key": "a8f1W000000TOAH"
58
}
59
],
60
"PROP65": [{
61
"value": "1GENERAL",
62
"name": "Code",
63
"key": "a8f6S000000btYS"
64
},
65
{
66
"value": "WARNING: Cancer and Reproductive Harm - www.P65Warnings.ca.gov.",
67
"name": "Short Warning",
68
"key": "a8f6S000000btYT"
69
}
70
],
71
"FP_PartType_F552": [{
72
"value": "15",
73
"name": "Length",
74
"key": "a8f6S000000Ynnr"
75
},
76
{
77
"value": "ABS with zinc die cast plugs",
78
"name": "Electric Cable Type",
79
"key": "a8f6S000000YnYr"
80
}
81
]
82
}}
83
Code 1:
JavaScript
1
5
1
import pandas as pd
2
3
term = data['specifications']['PKG&HAZMAT']
4
df = pd.json_normalize(term)[['name','value']]
5
Output:
JavaScript
1
11
11
1
print(df)
2
name value
3
0 VMRS FLETP
4
1 Sales Unit EA
5
2 Quantity per Application 0
6
3 Height Each 5.8
7
4 Width Each 20.35
8
5 Length Each 18.95
9
6 Weight Each 14.47
10
7 Prop 65 Statement WARNING Cancer and Reproductive Harm - www.P65
11
Code 2:
JavaScript
1
7
1
term = data['specifications']['PKG&HAZMAT']
2
for j in term:
3
name = j['name']
4
value = j['value']
5
6
print(name, value)
7
Output:
JavaScript
1
9
1
VMRS FLETP
2
Sales Unit EA
3
Quantity per Application 0
4
Height Each 5.8
5
Width Each 20.35
6
Length Each 18.95
7
Weight Each 14.47
8
Prop 65 Statement WARNING Cancer and Reproductive Harm - www.P65Warnings.ca.gov
9