0

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.

5
  • To translate between Python structures and JSON, use the standard library. Commented Jun 23, 2015 at 6:30
  • Yes: quotes (JSON uses " while python allows either single or double '' / ", string escaping etc. Do not attempt to write your own parser using string.replace(), just import the json module and use load() or loads(). Commented Jun 23, 2015 at 6:32
  • I'm not doing this in python. I'm generating a python script on a server running C#. Commented Jun 23, 2015 at 6:45
  • And you can't put a json string in the script? Commented Jun 23, 2015 at 6:54
  • That is what I currently have. But ideally i'd like to format it into valid code. This is designed as a tool to help people writing scripts against the API. i.e. I want to offer the ability to perform a list of actions using the UI. Then from the logs generate a script which only needs minor modification to be useable. Commented Jun 23, 2015 at 7:14

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I need to do this in non-python code. So can't use json.loads.
C#. I want to generate a valid python script on the server from a request log which can be downloaded and run by users.
1

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}

3 Comments

I need to do this in non-python code. So can't use json.loads.
@user2036256 You need to run it in a python script. With no python? I think you need to clarify your question because this makes no sense.
I want to generate a valid python script which a user can download from the server. Its designed as a tool to help users automate tasks using the API. I.e. you run actions against the UI then can download a script which can repeat them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.