File size: 578 Bytes
d31ff90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
"""Decode model JSON wrappers without changing, inventing or dropping document fields."""
import json
import re


def json_object(content):
    value=content
    for _ in range(5):
        if isinstance(value,dict):return value
        if isinstance(value,list) and len(value)==1:
            value=value[0]
        elif isinstance(value,str):
            text=re.sub(r'^```(?:json)?\s*|\s*```$','',value.strip(),flags=re.I)
            value=json.loads(text,strict=False)
        else:raise ValueError('Expected one JSON object')
    raise ValueError('Too many JSON wrappers')