Hector
Hector

Reputation: 1220

Differences between JSON & Python syntax

I have a request log from a server JSON REST API. I want to be able to turn this into a python script to replay that sequence of events.

Ideally i'd like the content to be easy to modify. So it would be perfect if I could actually translate the JSON into python Dictionaries/lists. i.e. From -

{"BoolVal":true, "SomeList":["a","b","c"]}

to

data = {"BoolVal":True, "SomeList":["a","b","c"]}

Are there any differences other than the true/false True/False that i'd need to be aware of?

*I need to do this on the server which does not have python. i.e. I want my users to be able to download a script to replay their actions.

Upvotes: 0

Views: 52

Answers (2)

juanchopanza
juanchopanza

Reputation: 227370

Presumably your JSON is a string, e.g. '{"BoolVal":true, "SomeList":["a","b","c"]}'.

You can load it into a python data structure using the json module:

>>> import json
>>> d = json.loads(json_string)
>>> print d
{u'SomeList': [u'a', u'b', u'c'], u'BoolVal': True}

Upvotes: 1

Neel
Neel

Reputation: 21243

If you have json then it will be like string

a = '''{"BoolVal":true, "SomeList":["a","b","c"]}'''

You can convert it using json module.

>>> import json
>>> json.loads(a)
{u'SomeList': [u'a', u'b', u'c'], u'BoolVal': True}

json.loads will return you valid python object.

Upvotes: 1

Related Questions