Python Forum

Full Version: Insert JSON into dict
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I want to add a string from a passed-in argument and insert it into a dict as a key-value.
The passed-in string is a piece of JSON, eg:
write_data('"body": {"storage":{"value":"<p>This is a new page</p>","representation":"storage"}}', 1234)

def write_data(my_str, pageid):
    data = {
    'id' : str(pageid),
    <ADD_MY_STR HERE>
}
so it looks like
def write_data(my_str, pageid):
    data = {
    'id' : str(pageid),
    "body": {"storage":{"value":"<p>This is a new page</p>","representation":"storage"}}
}
What is the question?  Please include any relevant code (within the code blocks) and complete error reports (within the error blocks).  What were your results, and what did you expect the results to be.
If it valid JSON then will json.loads(json_data) return a Python dictionary.
You update dictionary and can also dump i back to JSON with json.dumps(data).
Json doc.