penta penta - 14 days ago 6
JSON Question

Parsing Json array from an object in python

Sorry for the vague title, this is a basic question. I have a json(more of a dict) which looks like this.

{
'The Runaway Dinosaur':
['11th May 2016 ', ' 12:00am'],
'Paradox':
['12th Oct 2016 ', ' 12:00am'],
'Gorilla Warfare':
['18th Nov 2015 ', ' 1:00am'],
'Trajectory':
['23rd Mar 2016 ', ' 12:00am']
}


So each json object has an array of 2 elements, i want to extract the first element of each object.

I tried the following:-

for a in d: #d is the dict where the json is residing
print a[0]


Expected o/p :- 11th May 2016, 12th Oct 2016 ,...

Actual o/p :- T
P
G
T
R
T
F
E
R
T
P
T
T
T
K
M
P
G
F
T
V
F
O
T
T
F
T
R
R
P
F
W
G
R
T
I
S
B
C
K
S
A
F
L
T
F
I
C
T
F
T
E
M
F
W
T

I want to know what mistake am I making & how to rectify it?

Answer

When you try to iterate through dict you actually iterate through keys (which are strings so you get first character of each string-key) . And you need values. So:

for a in d.values():
    print a[0]