0

I have a json dumped string dumped in a file. Here is the file format -

{
u'key1':u'abc'
}

It's invalid json because its should have double quotes. How do I convert this into valid json?

2
  • 1
    You should have dumped this to file using the json module Commented Jan 16, 2018 at 12:05
  • 1
    This is not json indeed, this is a Python dict. Commented Jan 16, 2018 at 12:32

1 Answer 1

1

If you have read this data from your file:

s = """
{
    u'key1':u'abc'
}
"""

You may be able to convert it to a Python object using ast.literal_eval():

import ast
data = ast.literal_eval(s)
# data = {'key1': 'abc'}

Note that it might not work with other contents, because it seems you dumped a string (str() or repr()) representation of a dictionary to a file, instead of JSON. Other, more complex types may not be readable by ast.literal_eval(). You should use the json module instead to produce and write proper JSON to the file.

For reference:

As it seems you are working with Python 2:

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.