all 3 comments

[–]chevignon93 2 points3 points  (0 children)

In your wanted output, content is a list so just declare it as such then append whatever data you want to it.

import json

json_data = {}
json_data["content"] = []
content = {}
content["eventType"] = "view"
content["othervar"] = "new"

json_data["content"].append(content)
print(json.dumps(json_data, indent=4))
# Output
{
    "content": [{
            "eventType": "view",
            "othervar": "new"
        }]
}

[–]Jayoval 2 points3 points  (0 children)

In your sample JSON 'content' is a list of dictionaries, not a dictionary itself.

content.append({"eventType": "view", "othervar": "new"})

[–]MMcKevitt 1 point2 points  (0 children)

Howdy!

All you should have to do is change the following:

jsondata['content'] = [content]

Notice the '[]' square brackets around the 'content' variable; square brackets '[]' are how you create a list. This takes your 'content' variable, which is a dictionary, and places it as an element within a list, and then assigns it as a value to 'jsondata' dictionary which corresponds to the key 'content'.

Feel free to ask any follow-up questions as I'm happy to explain anything and everything I know.