Reputation: 959
This question clearly explains how to send a response using json and python dicts. The example however uses String as the value type in this dict. How would one do this with dict as value type? That is a dict with with dict as a value.
Upvotes: 0
Views: 685
Reputation: 600059
To clarify adityasdarma1's comment: this is not a limitation of Python or Django, but of JSON. In JSON, object keys must always be strings. There is no "tuple" type in JSON or JavaScript anyway; and arrays can't be keys because they are mutable. (In Python, tuples can be dict keys, but lists can't.)
I'm not sure why you would need that, though. You can either concatenate the values in some way to make a string key - eg "bar-baz"
- or alternatively you might need a more complex nested structure, with bar
as the key of the outer dict and baz
as an inner one. Without seeing your full data structure, it's hard to advise further.
Upvotes: 1
Reputation: 799560
By giving it a dict with a dict as a value type.
>>> json.dumps({'foo': {'bar': 42}})
'{"foo": {"bar": 42}}'
Upvotes: 1