0

I have output of a dictionary inside a list

[{'actors': 'Amy Poehler, Tina Fey, John Cena, Maya Rudolph',
'categories': {'id': 225, 'name': 'Comedy', 'parent_id': 2}, ...

I want the quotes to be " instead of '

The app I'm working on is supposed to return JSON and I believe for json ' is a character so it breaks it. Working okay! and not okay!

UPDATE

I've tried using json.dumps but it's being escaped

Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
Samuel Muiruri
  • 625
  • 1
  • 5
  • 13
  • 2
    Are you aware that that makes just about 0 difference? – Tim Jan 14 '16 at 13:26
  • I dont understand what are you trying to achieve. If you are using DRF, it serializes the output for you, so you dont have to do anything. How does your serializer look like? – yedpodtrzitko Jan 14 '16 at 13:54
  • @TimCastelijns apparently the OP wants to get *Django* to output the proper values. I've fixed the title for them. – Wayne Werner Jan 14 '16 at 13:55
  • 1
    Could you provide a [Minimal, Completed, Verifiable Example](http://stackoverflow.com/help/mcve) - it doesn't need to be your full Django app, but some small example beyond whatever the default Django start is. – Wayne Werner Jan 14 '16 at 13:57
  • so from my understanding, you want you response to be JSON, but the default content type produced by DRF is plaintext. Change you renderer class in your `ApiView` accordingly: class MyView(APIView): renderer_classes = (JSONRenderer, ) source: http://www.django-rest-framework.org/api-guide/renderers/ – yedpodtrzitko Jan 14 '16 at 14:02

2 Answers2

1

You can use the json lib

import json

array = [{'actors': 'Amy Poehler, Tina Fey, John Cena, Maya Rudolph', ...
print(json.dumps(array))
Rolbrok
  • 308
  • 1
  • 7
  • tried this and it's being escaped by django rest framework `"results": [ "{\"trailer_youtube_id\": \"vRnhEjP3R-c\", \"plot\": \"Two sisters decide to throw one last house party before their parents sell their family home.\", \"runtime\": \"118 min\", \"description\": \"Two sisters decide to throw one last house party before their parents sell their family home.\", \"videos\": [{\"youtube_id\": \"vRnhEjP3R-c\" ...` – Samuel Muiruri Jan 14 '16 at 13:45
  • Have you tried to output the results to a file ? It shouldn't output the escaped quotes. – Rolbrok Jan 14 '16 at 13:47
  • http://stage.kenyabuzz.com/rest/movies/?device=browser it's being delivered by rest framework json convert works but rest framework seems to escape the characters. – Samuel Muiruri Jan 14 '16 at 13:50
  • This seems normal, because if it didn't escape the characters, the `"{\"trailer_youtube_id\":` would be `"{"trailer_youtube_id":` and `trailer_youtube_id` would be out of the result and that would result in an error. – Rolbrok Jan 14 '16 at 13:54
  • By loading the file into the python json lib, it produces no error, it should the same on your json parser. – Rolbrok Jan 14 '16 at 13:57
0

As yedpodtrzitko mentioned, Django should be serializing the information for you - you don't have to turn it into a string first.

From the docs

content = JSONRenderer().render(serializer.data)
content
# '{"pk": 2, "title": "", "code": "print \\"hello, world\\"\\n", "linenos": false, "language": "python", "style": "friendly"}'

So it looks like whatever you get back from your serializer should just be a plain ol' dictionary with all of it's contents. I'm assuming that if you did

content = JSONRenderer().render({'quests': [{'Sir Galahad': 'I seek the grail',
                                             'King Arthur': 'I seek the grail'}])

that it would dump out the appropriate JSON string

Wayne Werner
  • 49,299
  • 29
  • 200
  • 290