0

I have a list of python dictionaries separated with commas in my file, i have to enclose these comma separated dictionaries in a python list to read my file as a json.

For Eg: myfile

{
    "title": "Sample Konfabulator Widget",
    "name": "main_window",
    "width": 500,
    "height": 500
},
{
    "title": "Sample Widget",
    "name": "main_window1",
    "width": 300,
    "height": 200
}

I have to make this as

[
{
    "title": "Sample Konfabulator Widget",
    "name": "main_window",
    "width": 500,
    "height": 500
},
{
    "title": "Sample Widget",
    "name": "main_window1",
    "width": 300,
    "height": 200
}
]

Any suggestions, how to achieve this?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Venkat J
  • 305
  • 1
  • 4
  • 12
  • Does this answer your question? [How do I write JSON data to a file?](https://stackoverflow.com/questions/12309269/how-do-i-write-json-data-to-a-file) – Karl Knechtel Aug 28 '22 at 21:19

1 Answers1

0

Have you tried something like this?

import json
d = json.loads('[{}]'.format(open('myfile').read())) 
slavos1
  • 56
  • 6
  • Not working, Getting below error IOError: [Errno 22] invalid mode ('r') or filename: – Venkat J Nov 22 '18 at 05:29
  • Are you sure you can open that myfile? And what you have in that `open()` exactly -- [IOError 22](https://stackoverflow.com/questions/15598160/ioerror-errno-22-invalid-mode-r-or-filename-c-python27-test-txt) may mean you have backslashes problem? – slavos1 Nov 22 '18 at 08:27