0
votes

This is my code

import json
data = '{"action_name": None, "execution_time": "00:00:00.000915", "timestamp": "2020-06-09T03:42:21.299294Z", "ip_address": None, "request": OrderedDict([("method", "GET"), ("full_path", "ggggg"), ("data", None), ("query_params", "{}")]), "response": OrderedDict([("status_code", 404), ("data", None)]), "user": OrderedDict([("id", None), ("username", None)])}'
json_data = json.loads(data)

The error is json.decoder.JSONDecodeError: Expecting value: line 1 column 17 (char 16)

raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 17 (char 16)
3

3 Answers

1
votes

In the data string, there is a python dictionary that in a sting representation and not a JSON format.
to convert the data to a dictionary you just need to use eval() and to import OrderedDict since you have it in the dictionary like

from collections import OrderedDict    

json_data = eval(data)

That will convert the data string to a dictionary as

{'action_name': None,
 'execution_time': '00:00:00.000915',
 'timestamp': '2020-06-09T03:42:21.299294Z',
 'ip_address': None,
 'request': OrderedDict([('method', 'GET'),
              ('full_path', 'ggggg'),
              ('data', None),
              ('query_params', '{}')]),
 'response': OrderedDict([('status_code', 404), ('data', None)]),
 'user': OrderedDict([('id', None), ('username', None)])}
0
votes

you can do this, json documentation.

import json

data = '{"action_name": None, "execution_time": "00:00:00.000915", "timestamp": "2020-06-09T03:42:21.299294Z", "ip_address": None, "request": OrderedDict([("method", "GET"), ("full_path", "ggggg"), ("data", None), ("query_params", "{}")]), "response": OrderedDict([("status_code", 404), ("data", None)]), "user": OrderedDict([("id", None), ("username", None)])}'
json_data = json.dumps(data)
0
votes

This worked for me, posting for reference

import json
from collections import OrderedDict
data = '{"action_name": None, "execution_time": "00:00:00.000915", "timestamp": "2020-06-09T03:42:21.299294Z", "ip_address": None, "request": OrderedDict([("method", "GET"), ("full_path", "ggggg"), ("data", None), ("query_params", "{}")]), "response": OrderedDict([("status_code", 404), ("data", None)]), "user": OrderedDict([("id", None), ("username", None)])}'
json_data = json.dumps(data, sort_keys=True, indent=4)
dict_json = eval(json.loads(json_data))
print(dict_json['request'])