Home > Software design >  Parsing json string and json obj in same json obj in Python
Parsing json string and json obj in same json obj in Python

Time:07-04

there's a json obj:

{'key1': 'val1', 'key2': "{"key3": ["val3"]}"}

how to parse json string and json obj in the same json obj

json.load() => AttributeError: 'str' object has no attribute 'read' json.loads() => JSONDecodeError: Expecting ',' delimiter: line 1 column 232 (char 231)

CodePudding user response:

Pretty sure you want to use JSON library as Himanshu suggested.

I think this will answer your questions: enter image description here enter image description here

CodePudding user response:

Easiest way is to format the dict with multi-line string, then use json.dumps:

d = {'key1': 'val1', 'key2': """{"key3": ["val3"]}"""}
json.dumps(d) # a new json string
json.loads(d["key2"]) # the obj
  • Related