1

I have a dictionary that specified a layout for a map I am going to use in javascript. The only thing I need to do to make this dictionary a valid json is to convert the single ' to ".

I am new to java and json stuff but here is a snippet of what the dictionary loos like:

    {'State': '336', 'Variable': 'Median Household Income', 'Year': 
'2017'}, 'below': 'water', 'type': 'fill', 'color': 'rgba(187, 214, 235, 
255)', 'maxzoom': 13, 'opacity': 0.7}], 'bearing': 0, 'center': {'lat': 
45.51439913430827, 'lon': -122.76771709039427}, 'pitch': 0, 'zoom': 9, 
'style': 'streets'}, 'margin': {'l': 0, 'r': 0, 't': 0, 'b': 0}, 
'plot_bgcolor': '#202d56', 'paper_bgcolor': '#202d56'}
Snorrlaxxx
  • 168
  • 1
  • 3
  • 18
  • 2
    A `dict` is never JSON. A dict is a materialized data structure, *there are no quotes in it*, that is merely how it is represented when it is printed. On the other hand, JSON is a text-based serialization format. To *serialize* a Python data structure, like a `dict` into a valid JSON string, you can use the `json` module. – juanpa.arrivillaga Nov 05 '19 at 18:10
  • @juanpa.arrivillaga Thank you can you provide an answer? I really just need to figure out a way of replacing ' to ". – Snorrlaxxx Nov 05 '19 at 18:20
  • 1
    I linked a duplicate. use `import json` then either `json.dumps(my_dict)` to create a json `str` or `json.dump(my_dict, file_handle)` to dump to a file. Here's the docs: https://docs.python.org/3/library/json.html – juanpa.arrivillaga Nov 05 '19 at 18:27
  • @juanpa.arrivillaga json.dumps does not work and returns this:SyntaxError: Unexpected end of JSON input at JSON.parse () at ChildProcess. (C:\Users\Morgan.Weiss\Documents\workspace\pa-web-app\app\routes.js:637:31) at ChildProcess.emit (events.js:198:13) at maybeClose (internal/child_process.js:982:16) at Socket.stream.socket.on (internal/child_process.js:389:11) at Socket.emit (events.js:198:13) at Pipe._handle.close (net.js:607:12) – Snorrlaxxx Nov 05 '19 at 18:28
  • You need to provide a [mcve]. `json.dumps` **definitely works**, but you need to show exactly what you are doing. – juanpa.arrivillaga Nov 05 '19 at 18:29
  • Thanks but I made a new question to address what I exactly need. – Snorrlaxxx Nov 05 '19 at 18:30

0 Answers0