Package: json-duplicate-keys
JSON Duplicate KeysFlatten/ Unflatten and Load(s)/ Dump(s) JSON File/ Object with Duplicate KeysInstallationFrom PyPI:pip install json-duplicate-keysFrom Source:git clone https://github.com/truocphan/json-duplicate-keys.git --branch <Branch/Tag>cd json-duplicate-keyspython setup.py buildpython setup.py installBasic Usagenormalize_key(name,dupSign_start="{{{",dupSign_end="}}}",_isDebug_=False)Normalize Key namename: key namedupSign_start:dupSign_end:_isDebug_: Show/ Hide debug error messagesimportjson_duplicate_keysasjdksprint(jdks.normalize_key("version{{{_2_}}}"))# OUTPUT: versionloads(Jstr,dupSign_start="{{{",dupSign_end="}}}",ordered_dict=False,_isDebug_=False)Deserialize a JSON format string to a classJSON_DUPLICATE_KEYSJstr: a JSON format stringdupSign_start:dupSign_end:ordered_dict: preserves the order in which the Keys are inserted_isDebug_: Show/ Hide debug error messagesimportjson_duplicate_keysasjdksJstr='{"author": "truocphan", "version": "22.3.3", "version": "latest", "release": [{"version": "latest"}], "snapshot": {"author": "truocphan", "version": "22.3.3", "release": [{"version": "latest"}]}}'JDKSObject=jdks.loads(Jstr)print(JDKSObject)# OUTPUT: <json_duplicate_keys.JSON_DUPLICATE_KEYS object at 0x00000270AE987940>load(Jfilepath,dupSign_start="{{{",dupSign_end="}}}",ordered_dict=False,_isDebug_=False)Deserialize a JSON format string from a file to a classJSON_DUPLICATE_KEYSJfilepath: The path to the file containing the JSON format stringdupSign_start:dupSign_end:ordered_dict: preserves the order in which the Keys are inserted_isDebug_: Show/ Hide debug error messages# /path/to/file.json: {"author": "truocphan", "version": "22.3.3", "version": "latest", "release": [{"version": "latest"}], "snapshot": {"author": "truocphan", "version": "22.3.3", "release": [{"version": "latest"}]}}importjson_duplicate_keysasjdksJfilepath="/path/to/file.json"JDKSObject=jdks.load(Jfilepath)print(JDKSObject)# OUTPUT: <json_duplicate_keys.JSON_DUPLICATE_KEYS object at 0x00000270AE986D40>JSON_DUPLICATE_KEYS.getObject()Get the JSON objectimportjson_duplicate_keysasjdksJstr='{"author": "truocphan", "version": "22.3.3", "version": "latest", "release": [{"version": "latest"}], "snapshot": {"author": "truocphan", "version": "22.3.3", "release": [{"version": "latest"}]}}'JDKSObject=jdks.loads(Jstr)print(JDKSObject.getObject())# OUTPUT: {'author': 'truocphan', 'version': '22.3.3', 'version{{{_2_}}}': 'latest', 'release': [{'version': 'latest'}], 'snapshot': {'author': 'truocphan', 'version': '22.3.3', 'release': [{'version': 'latest'}]}}JSON_DUPLICATE_KEYS.get(name,separator="||",parse_index="$",_isDebug_=False)Get value in the JSON object bynamename: the key name of the JSON object. Supported flatten key name formatseparator:parse_index:_isDebug_: Show/ Hide debug error messagesimportjson_duplicate_keysasjdksJstr='{"author": "truocphan", "version": "22.3.3", "version": "latest", "release": [{"version": "latest"}], "snapshot": {"author": "truocphan", "version": "22.3.3", "release": [{"version": "latest"}]}}'JDKSObject=jdks.loads(Jstr)print(JDKSObject.get("version{{{_2_}}}"))# OUTPUT: latestprint(JDKSObject.get("release||$0$"))# OUTPUT: {'version': 'latest'}print(JDKSObject.get("snapshot||author"))# OUTPUT: truocphanJSON_DUPLICATE_KEYS.set(name,value,separator="||",parse_index="$",dupSign_start="{{{",dupSign_end="}}}",ordered_dict=False,_isDebug_=False)Set a newnameandvaluefor the JSON objectname: new key name for the JSON object. Supported flat key name formatvalue: value for keynameseparator:parse_index:dupSign_start:dupSign_end:ordered_dict: preserves the order in which the Keys are inserted_isDebug_: Show/Hide debug error messagesimportjson_duplicate_keysasjdksJstr='{}'JDKSObject=jdks.loads(Jstr)print(JDKSObject.getObject())# OUTPUT: {}JDKSObject.set("author","truocphan")print(JDKSObject.getObject())# OUTPUT: {'author': 'truocphan'}JDKSObject.set("version","22.3.3")print(JDKSObject.getObject())# OUTPUT: {'author': 'truocphan', 'version': '22.3.3'}JDKSObject.set("version","latest")print(JDKSObject.getObject())# OUTPUT: {'author': 'truocphan', 'version': '22.3.3', 'version{{{_2_}}}': 'latest'}JDKSObject.set("release",[{"version":"latest"}])print(JDKSObject.getObject())# OUTPUT: {'author': 'truocphan', 'version': '22.3.3', 'version{{{_2_}}}': 'latest', 'release': [{'version': 'latest'}]}JDKSObject.set("snapshot",{})print(JDKSObject.getObject())# OUTPUT: {'author': 'truocphan', 'version': '22.3.3', 'version{{{_2_}}}': 'latest', 'release': [{'version': 'latest'}], 'snapshot': {}}JDKSObject.set("snapshot||author","truocphan")print(JDKSObject.getObject())# OUTPUT: {'author': 'truocphan', 'version': '22.3.3', 'version{{{_2_}}}': 'latest', 'release': [{'version': 'latest'}], 'snapshot': {'author': 'truocphan'}}Jstr='[]'JDKSObject=jdks.loads(Jstr)print(JDKSObject.getObject())# OUTPUT: []JDKSObject.set("author","truocphan")print(JDKSObject.getObject())# OUTPUT: [{'author': 'truocphan'}]JDKSObject.set("release",[])print(JDKSObject.getObject())# OUTPUT: [{'author': 'truocphan'}, {'release': []}]JDKSObject.set("$1$||release||",{"version":"latest"})print(JDKSObject.getObject())# OUTPUT: [{'author': 'truocphan'}, {'release': [{'version': 'latest'}]}]JSON_DUPLICATE_KEYS.update(name,value,separator="||",parse_index="$",_isDebug_=False)Update newvaluefor existingnamein the JSON objectname: the key name of the JSON object. Supported flatten key name formatvalue: new value for keynameseparator:parse_index:_isDebug_: Show/ Hide debug error messagesimportjson_duplicate_keysasjdksJstr='{"author": "truocphan", "version": "22.3.3", "version": "latest", "release": [{"version": "latest"}], "snapshot": {"author": "truocphan", "version": "22.3.3", "release": [{"version": "latest"}]}}'JDKSObject=jdks.loads(Jstr)print(JDKSObject.getObject())# OUTPUT: {'author': 'truocphan', 'version': '22.3.3', 'version{{{_2_}}}': 'latest', 'release': [{'version': 'latest'}], 'snapshot': {'author': 'truocphan', 'version': '22.3.3', 'release': [{'version': 'latest'}]}}JDKSObject.update("version{{{_2_}}}",["22.3.3","latest"])JDKSObject.update("snapshot||version","latest")print(JDKSObject.getObject())# OUTPUT: {'author': 'truocphan', 'version': '22.3.3', 'version{{{_2_}}}': ['22.3.3', 'latest'], 'release': [{'version': 'latest'}], 'snapshot': {'author': 'truocphan', 'version': 'latest', 'release': [{'version': 'latest'}]}}JSON_DUPLICATE_KEYS.delete(name,separator="||",parse_index="$",_isDebug_=False)Delete a key-value pair in a JSON object by keynamename: the key name of the JSON object. Supported flatten key name formatseparator:parse_index:_isDebug_: Show/ Hide debug error messagesimportjson_duplicate_keysasjdksJstr='{"author": "truocphan", "version": "22.3.3", "version": "latest", "release": [{"version": "latest"}], "snapshot": {"author": "truocphan", "version": "22.3.3", "release": [{"version": "latest"}]}}'JDKSObject=jdks.loads(Jstr)print(JDKSObject.getObject())# OUTPUT: {'author': 'truocphan', 'version': '22.3.3', 'version{{{_2_}}}': 'latest', 'release': [{'version': 'latest'}], 'snapshot': {'author': 'truocphan', 'version': '22.3.3', 'release': [{'version': 'latest'}]}}JDKSObject.delete("version")JDKSObject.delete("release||$0$")JDKSObject.delete("snapshot")print(JDKSObject.getObject())# OUTPUT: {'author': 'truocphan', 'version{{{_2_}}}': 'latest', 'release': []}JSON_DUPLICATE_KEYS.filter_keys(name,separator="||",parse_index="$",ordered_dict=False)name:separator:parse_index:ordered_dict:importjson_duplicate_keysasjdksJstr='{"author": "truocphan", "version": "22.3.3", "version": "latest", "release": [{"version": "latest"}], "snapshot": {"author": "truocphan", "version": "22.3.3", "release": [{"version": "latest"}]}}'JDKSObject=jdks.loads(Jstr)print(JDKSObject.filter_keys("version").dumps())# OUTPUT: {"version": "22.3.3", "version": "latest", "release||$0$||version": "latest", "snapshot||version": "22.3.3", "snapshot||release||$0$||version": "latest"}print(JDKSObject.dumps())# OUTPUT: {"author": "truocphan", "version": "22.3.3", "version": "latest", "release": [{"version": "latest"}], "snapshot": {"author": "truocphan", "version": "22.3.3", "release": [{"version": "latest"}]}}JSON_DUPLICATE_KEYS.dumps(dupSign_start="{{{",dupSign_end="}}}",_isDebug_=False,skipkeys=False,ensure_ascii=True,check_circular=True,allow_nan=True,cls=None,indent=None,separators=None,default=None,sort_keys=False)Serialize a JSON object to a JSON format stringdupSign_start:dupSign_end:_isDebug_: Show/ Hide debug error messagesFor remaining arguments, please refer tojson.dump()importjson_duplicate_keysasjdksJstr='{"author": "truocphan", "version": "22.3.3", "version": "latest", "release": [{"version": "latest"}], "snapshot": {"author": "truocphan", "version": "22.3.3", "release": [{"version": "latest"}]}}'JDKSObject=jdks.loads(Jstr)print(JDKSObject.getObject())# OUTPUT: {'author': 'truocphan', 'version': '22.3.3', 'version{{{_2_}}}': 'latest', 'release': [{'version': 'latest'}], 'snapshot': {'author': 'truocphan', 'version': '22.3.3', 'release': [{'version': 'latest'}]}}JDKSObject.delete("version")JDKSObject.delete("release||$0$")JDKSObject.delete("snapshot")print(JDKSObject.dumps())# OUTPUT: {"author": "truocphan", "version": "latest", "release": []}JSON_DUPLICATE_KEYS.dump(Jfilepath,dupSign_start="{{{",dupSign_end="}}}",_isDebug_=False,skipkeys=False,ensure_ascii=True,check_circular=True,allow_nan=True,cls=None,indent=None,separators=None,default=None,sort_keys=False)Serialize a JSON object to a JSON format string and write to a fileJfilepath: the path to the file to save the JSON format stringdupSign_start:dupSign_end:_isDebug_: Show/ Hide debug error messagesFor remaining arguments, please refer tojson.dump()importjson_duplicate_keysasjdksJstr='{"author": "truocphan", "version": "22.3.3", "version": "latest", "release": [{"version": "latest"}], "snapshot": {"author": "truocphan", "version": "22.3.3", "release": [{"version": "latest"}]}}'JDKSObject=jdks.loads(Jstr)print(JDKSObject.getObject())# OUTPUT: {'author': 'truocphan', 'version': '22.3.3', 'version{{{_2_}}}': 'latest', 'release': [{'version': 'latest'}], 'snapshot': {'author': 'truocphan', 'version': '22.3.3', 'release': [{'version': 'latest'}]}}JDKSObject.delete("version")JDKSObject.delete("release||$0$")JDKSObject.delete("snapshot")Jfilepath="/path/to/file.json"JDKSObject.dump(Jfilepath)JDKSObject_load=jdks.load(Jfilepath)print(JDKSObject_load.getObject())# OUTPUT: {'author': 'truocphan', 'version': 'latest', 'release': []}JSON_DUPLICATE_KEYS.flatten(separator="||",parse_index="$",ordered_dict=False,_isDebug_=False)Flatten a JSON object to a single key-value pairsseparator:parse_index:ordered_dict: preserves the order in which the Keys are inserted_isDebug_: Show/ Hide debug error messagesimportjson_duplicate_keysasjdksJstr='{"author": "truocphan", "version": "22.3.3", "version": "latest", "release": [{"version": "latest"}], "snapshot": {"author": "truocphan", "version": "22.3.3", "release": [{"version": "latest"}]}}'JDKSObject=jdks.loads(Jstr)print(JDKSObject.getObject())# OUTPUT: {'author': 'truocphan', 'version': '22.3.3', 'version{{{_2_}}}': 'latest', 'release': [{'version': 'latest'}], 'snapshot': {'author': 'truocphan', 'version': '22.3.3', 'release': [{'version': 'latest'}]}}JDKSObject.flatten()print(JDKSObject.getObject())# OUTPUT: {'author': 'truocphan', 'version': '22.3.3', 'version{{{_2_}}}': 'latest', 'release||$0$||version': 'latest', 'snapshot||author': 'truocphan', 'snapshot||version': '22.3.3', 'snapshot||release||$0$||version': 'latest'}JSON_DUPLICATE_KEYS.unflatten(separator="||",parse_index="$",ordered_dict=False,_isDebug_=False)Unflatten a flattened JSON object back to a JSON objectseparator:parse_index:ordered_dict: preserves the order in which the Keys are inserted_isDebug_: Show/ Hide debug error messagesimportjson_duplicate_keysasjdksJstr='{"author": "truocphan", "version": "22.3.3", "version": "latest", "release||$0$||version": "latest", "snapshot||author": "truocphan", "snapshot||version": "22.3.3", "snapshot||release||$0$||version": "latest"}'JDKSObject=jdks.loads(Jstr)print(JDKSObject.getObject())# OUTPUT: {'author': 'truocphan', 'version': '22.3.3', 'version{{{_2_}}}': 'latest', 'release||$0$||version': 'latest', 'snapshot||author': 'truocphan', 'snapshot||version': '22.3.3', 'snapshot||release||$0$||version': 'latest'}JDKSObject.unflatten()print(JDKSObject.getObject())# OUTPUT: {'author': 'truocphan', 'version': '22.3.3', 'version{{{_2_}}}': 'latest', 'release': [{'version': 'latest'}], 'snapshot': {'author': 'truocphan', 'version': '22.3.3', 'release': [{'version': 'latest'}]}}