I'm trying to create a DataFrame object from a json string (not a file):
json_string = "[{'code': '8', 'name': 'Human'}, {'code': '11', 'name': 'Orc'}]"
df = pd.read_json(json_string)
but this approach causes the following error:
ValueError: Expected object or value
The documentation makes it sound like this would be possible:
pandas.read_json(path_or_buf=None, orient=None, typ='frame', dtype=True, convert_axes=True, convert_dates=True, keep_default_dates=True, numpy=False, precise_float=False, date_unit=None, encoding=None, lines=False, chunksize=None, compression='infer')
Convert a JSON string to pandas object
- path_or_buf : a valid JSON string or file-like, default: None The string could be a URL. Valid URL schemes include http, ftp, s3, and file. For file URLs, a host is expected. For instance, a local file could be file://localhost/path/to/table.json
I've already tried calling the method with several combinations of orient and none succeeded. Any tips on how to accomplish this?
I really don't want the overhead of saving my string to a file to be able to use read_json() successfully.
keysandvaluesshould be enclosed with double quotes not single.evalto convert it tolistofdictjson_string = json_string.replace('\'', '\"')