commit 6593843cd36c9b7f73bc9060d6ed85aee666e7e0 Author: Hazel Date: Tue Jul 2 12:41:08 2024 +0000 Add Home diff --git a/Home.md b/Home.md new file mode 100644 index 0000000..fcc7c00 --- /dev/null +++ b/Home.md @@ -0,0 +1,45 @@ +# How it works + +## create object with json string as value + +```python +>>> o = {"foo": "foo"} +>>> oo = {"bar": json.dumps(o)} +>>> oo +{'bar': '{"foo": "foo"}'} +>>> json.dumps(oo) +'{"bar": "{\\"foo\\": \\"foo\\"}"}' +``` + +## extract the escaped string + +```python +>>> s = json.dumps(oo)[9:-2] +>>> s +'{\\"foo\\": \\"foo\\"}' +``` + +## reproduce error + +```python +>>> r = json.loads(s) +Traceback (most recent call last): + File "", line 1, in + File "/usr/lib/python3.10/json/__init__.py", line 346, in loads + return _default_decoder.decode(s) + File "/usr/lib/python3.10/json/decoder.py", line 337, in decode + obj, end = self.raw_decode(s, idx=_w(s, 0).end()) + File "/usr/lib/python3.10/json/decoder.py", line 353, in raw_decode + obj, end = self.scan_once(s, idx) +json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1) +``` + +## solve error by nesting json + +```python +>>> r = json.loads('{"foo": "' + s + '"}') +>>> r +{'foo': '{"foo": "foo"}'} +>>> json.loads(r["foo"]) +{'foo': 'foo'} +``` \ No newline at end of file