0

I can create the following json format:

>>> json_response = {}
>>> json_response["data"] = {}
>>> json_response["data"][0] = "bla"
>>> print(json.dumps(json_response))
{"data": {"0": "bla"}}

But what I need is this final result:

{"data": [{"0": "bla"}]}

How do I get there?

1
  • With json_response = {"data": [{"0": "bla"}]}. Or just print('{"data": [{"0": "bla"}]}'). Commented Mar 13, 2021 at 2:53

1 Answer 1

1

Instead of defining json_response["data"] = {} as a dictionary define it as a list json_response["data"] = [] and then append your dict to it.

json_response = {}
json_response["data"] = []
json_response["data"].append({0:"bla"})
print(json.dumps(json_response))
Sign up to request clarification or add additional context in comments.

Comments

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.