How to print specific json list items?

I'm making a python bot and the response is coming back as JSON.

Here is a quick show of what it brings back:

[["Message","User string from here."]]

So first what I've done is, loaded the json from the python module json.

json.loads(resp)

and it brings back:

[[u"Message",u"user string from here"]]

How do I print out the Message which will return the value user string from here?

Upvotes: 1

Views: 5948

Answers (2)

Animesh Sharma
Animesh Sharma

Reputation: 3386

json.loads converts json into the native datatypes of python. So, when you did json.loads(resp), you get back a list of lists.

You can simply iterate over the list to access whatever elements you want.

In your case, print json.loads(resp)[0][1] should suffice.

Upvotes: 0

Cyphase
Cyphase

Reputation: 12022

All you need to do is:

from __future__ import print_function

import json

raw_response = '[["Message","User string from here."]]'
data = json.loads(raw_response)

print(data[0][1])

Upvotes: 2

Related Questions