I have a json file like below
JavaScript
x
19
19
1
{
2
3
"sample": 100,
4
"columns": [{
5
"name": "col1",
6
"value": 11
7
},
8
{
9
"name": "col2",
10
"value": 210
11
},
12
..
13
{
14
"name": "col10",
15
"value": 20
16
}
17
]
18
}
19
I need to find the position of name: col10
in the columns array.
Advertisement
Answer
something like the below
JavaScript
1
25
25
1
data = {
2
3
"sample": 100,
4
"columns": [{
5
"name": "col1",
6
"value": 11
7
},
8
{
9
"name": "col2",
10
"value": 210
11
},
12
{
13
"name": "col10",
14
"value": 20
15
}
16
]
17
}
18
19
col_name = 'col2'
20
for idx, entry in enumerate(data['columns']):
21
if entry['name'] == col_name:
22
print(f'index of {col_name} is {idx}')
23
# assuming it can be found once - break
24
break
25
output
JavaScript
1
2
1
index of col2 is 1
2