Reputation: 119
I am tryinging to create a nested Json structure as follows:
Example Json:
{
"id" : "de",
"Key" : "1234567",
"from" : "[email protected]",
"expires" : "2018-04-25 18:45:48.3166159",
"command" : "method.exec",
"params" : {
"method" : "cmd",
"Key" : "default",
"params" : {
"command" : "testing 23"
}
}
I am trying to do this from an OrderedDict. I am not sure of the correct way to structure the OrderedDict so that the correct Json is produced.
Python Code:
json_payload = OrderedDict(
[('id', id),
('Key', keystore),
('from', '[email protected]'),
('expires', expires),
('command', 'method.exec')]
# What goes here for the params section??
)
print json.dumps(json_payload, indent=4, default=str)
Upvotes: 4
Views: 18840
Reputation: 360
Thanks to the answer from @Nids Barthwal I was able to get around limitations of the json_script template tag. I used that JSONRenderer strategy to create a custom tag:
@register.filter(name='ordereddict_to_json')
def ordereddict_to_json(value):
result = JSONRenderer().render(value)
output_stream = io.BytesIO(result)
data = JSONParser().parse(output_stream)
return json.dumps(data)
... once I plugged it into a data attribute...
<div id="foo" data-datavalue='{{ contextThing|ordereddict_to_json }}'>
// Something dynamically rendered in javascript
</div>
... it was easy to pick it up on the front end:
function renderMyThing(thing) {
let dataValue = thing.data("datavalue")
let htmlString = `This part: ${dataValue.something} and also this other part: ${dataValue.somethingElse}`
// ...
}
Upvotes: 0
Reputation: 2419
WORKING !!!
Mostly When We serialize querysets instead of model instances
Ex :
serialized_data = SnippetSerializer(MyModel.objects.all(), many=True)
Output :
[
OrderedDict([('code', 'ABC'), ('quantity', 5.0)]),
OrderedDict([('code', 'GGG'), ('quantity', 4.0)])
]
We can convert it to json like this : -
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
import io
result = JSONRenderer().render(serialized_data)
output_stream = io.BytesIO(result)
data = JSONParser().parse(output_stream)
print(data)
Output :
[
{'code': 'ABC', 'quantity': 5.0},
{'code': 'GGG', 'quantity': 4.0}
]
Upvotes: 2
Reputation: 119
Using @haifzhan's output as the input delivered exactly what was required.
payload = OrderedDict(
[
('id', 'de'),
('Key', '1234567'),
('from', '[email protected]'),
('expires', '2018-04-25 18:45:48.3166159'),
('command', 'method.exec'),
('params',
OrderedDict(
[
('method', 'cmd'),
('Key', 'default'),
('params',
OrderedDict(
[
('command', 'testing 23')
]
)
)
]
)
)
]
)
print json.dumps(json_payload, indent=4, default=str)
Upvotes: 5
Reputation: 31915
You missed a }
at the end of your JSON data.
import json
import collections
data = {
"id" : "de",
"Key" : "1234567",
"from" : "[email protected]",
"expires" : "2018-04-25 18:45:48.3166159",
"command" : "method.exec",
"params" : {
"method" : "cmd",
"Key" : "default",
"params" : {
"command" : "testing 23"
}
}}
data_str = json.dumps(data)
result = json.loads(data_str, object_pairs_hook=collections.OrderedDict)
print(result)
Output:
OrderedDict(
[
('id', 'de'),
('Key', '1234567'),
('from', '[email protected]'),
('expires', '2018-04-25 18:45:48.3166159'),
('command', 'method.exec'),
('params',
OrderedDict(
[
('method', 'cmd'),
('Key', 'default'),
('params',
OrderedDict(
[
('command', 'testing 23')
]
)
)
]
)
)
]
)
Upvotes: 4
Reputation: 3553
A few things. id
is a keyword. You can just pass a dictionary as a parameter.
ids = "de"
keystore = "1234567"
expires = "2018-04-25 18:45:48.3166159"
pdict = {
"method" : "cmd",
"Key" : "default",
"params" : {
"command" : "testing 23"
}
}
json_payload = OrderedDict(
[('id', id),
('Key', keystore),
('from', '[email protected]'),
('expires', expires),
('command', 'method.exec'),
('params',pdict )
]
)
print(json.dumps(json_payload, indent=4, default=str))
Upvotes: 2