0

I've run into an issue wherein I get a formatted message created by these util functions:

Example output:

MSG(variable_group=MSG(x=0,y=0,z=0), variable1='name', variable2=1)

Here are the functions used to make the message.

def _json_object_hook(d):
    return namedtuple("MSG", d.keys())(*d.values())


def json2obj(data):
    return json.loads(data, object_hook=_json_object_hook)

I want to convert the above output back into a json object to convert into a python dictionary, but I am unsure how to go about that with the object_hook and namedtuple.

tvanfossen
  • 25
  • 8
  • from named_tuple to json try this: https://stackoverflow.com/questions/5906831/serializing-a-python-namedtuple-to-json – Druta Ruslan May 23 '18 at 19:29
  • "*I want to convert the above output back into a json object to convert into a python dictionary,*" -- I suspect it will be easier to convert to a dictionary directly. Do you require the extra conversion step? – Robᵩ May 23 '18 at 19:32

1 Answers1

0

Assumed this would require a new thread as the topic changed when the namedtuple was found to be involved.

Parse JSON MSG in Python

tvanfossen
  • 25
  • 8