This is JSON:
JavaScript
x
21
21
1
"y": {
2
"titleOne": {
3
"a": [{
4
"ss": "one"
5
}, {
6
"ss": "two"
7
}
8
]
9
},
10
"titleTwo": {
11
"a": [{
12
"ss": "one"
13
}, {
14
"ss": "two"
15
}, {
16
"ss": "thee"
17
}
18
]
19
},
20
.
21
This is my current code:
JavaScript
1
4
1
for i in y:
2
for c in y[i]["a"]:
3
print(c["ss"])
4
This code will simply print all values in order:
JavaScript
1
6
1
one
2
two
3
one
4
two
5
three
6
but I need that loop will get the first value from each section and will return
JavaScript
1
6
1
one
2
one
3
two
4
two
5
three
6
Advertisement
Answer
Maybe something like this:
JavaScript
1
16
16
1
y = {"titleOne": {"a": [{"ss": "one"}, {"ss": "two"}]},
2
"titleTwo": {"a": [{"ss": "one"}, {"ss": "two"}, {"ss": "three"}]}}
3
4
#check the max depth you can go to
5
depth = max(len(y[title]["a"]) for title in y)
6
for i in range(depth):
7
for title in y:
8
if len(y[title]["a"])>i:
9
print(y[title]["a"][i]["ss"])
10
11
one
12
one
13
two
14
two
15
three
16