-2

Got an array with objects called data that looks something like this:

[
   {
       'type': 'motorcycle', 
       'properties': [ 
           {
               'type': 'large', 
               'brand': 'kawasaki', 
               'attribute': {
                    'id': '98-fda7d7fa', 
                    'title': 'Ninja'
                },
            }
         ]
    }
[

I can output a json file like this:

with open('output.json', 'w') as f:
    print(data, file=f)        

but it comes out with single quotes and not double quotes,

so I can't really automatically format it on my code editor

I am trying to follow the top voted answer to fix that:

with open('output.json', 'w') as f:
    print(json.load(data), file=f)

But I get this error:

AttributeError: 'list' object has no attribute 'read'
uber
  • 4,163
  • 5
  • 26
  • 55

1 Answers1

1

json.load reads JSON already stored in a file, and converts it into a Python object. You want json.dump:

with open('output.json', 'w') as f:
    json.dump(data, f)
Michael Wheeler
  • 849
  • 1
  • 10
  • 29