Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Commit fc61da8

Browse files
committed
Added new function: JSON_DUPLICATE_KEYS.flatten()
1 parent 95d5104 commit fc61da8

File tree

2 files changed

+73
-46
lines changed

2 files changed

+73
-46
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,31 @@ print(JDKSObject_load.getObject())
230230

231231
### JSON_DUPLICATE_KEYS.flatten(`separator`="||", `parse_index`="$", `ordered_dict`=False, `_isDebug_`=True)
232232
_Flatten a JSON object to a single key-value pairs_
233+
- `separator`:
234+
- `parse_index`:
235+
- `ordered_dict`: preserves the order in which the Keys are inserted
236+
- `_isDebug_`: Show/ Hide debug error messages
233237
```python
238+
import json_duplicate_keys as jdks
239+
240+
Jstr = '{"author": "truocphan", "version": "22.3.3", "version": "latest", "release": [{"version": "latest"}], "snapshot": {"author": "truocphan", "version": "22.3.3", "release": [{"version": "latest"}]}}'
241+
242+
JDKSObject = jdks.loads(Jstr)
243+
244+
print(JDKSObject.getObject())
245+
# OUTPUT: {'author': 'truocphan', 'version': '22.3.3', 'version{{{_2_}}}': 'latest', 'release': [{'version': 'latest'}], 'snapshot': {'author': 'truocphan', 'version': '22.3.3', 'release': [{'version': 'latest'}]}}
246+
247+
JDKSObject.flatten()
248+
249+
print(JDKSObject.getObject())
250+
# 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'}
234251
```
235252

236253
### JSON_DUPLICATE_KEYS.unflatten(`separator`="||", `parse_index`="$", `ordered_dict`=False, `_isDebug_`=True)
237254
_Unflatten a flattened JSON object back to a JSON object_
255+
- `separator`:
256+
- `parse_index`:
257+
- `ordered_dict`: preserves the order in which the Keys are inserted
258+
- `_isDebug_`: Show/ Hide debug error messages
238259
```python
239260
```

json_duplicate_keys/__init__.py

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -325,64 +325,70 @@ def dump(self, Jfilepath, dupSign_start="{{{", dupSign_end="}}}", _isDebug_=True
325325
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
326326

327327

328-
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
329-
# # # # # # # # # # # # # # flatten # # # # # # # # # # # # # #
330-
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
331-
# def flatten(self, separator="||", parse_index="$", ordered_dict=False):
332-
# from collections import OrderedDict
328+
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
329+
# # # # # # # # # # # # # flatten # # # # # # # # # # # # # #
330+
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
331+
def flatten(self, separator="||", parse_index="$", ordered_dict=False, _isDebug_=True):
332+
from collections import OrderedDict
333333

334-
# if len(self.__Jobj) > 0:
335-
# Jflat = dict()
336-
# if ordered_dict:
337-
# Jflat = OrderedDict()
338-
339-
# def __convert_Jobj_to_Jflat(Jobj, key=None):
340-
# if type(Jobj) in [dict, OrderedDict]:
341-
# if len(Jobj) == 0:
342-
# Jflat[key] = dict()
343-
# if ordered_dict:
344-
# Jflat[key] = OrderedDict()
345-
# else:
346-
# for k,v in Jobj.items():
347-
# _Jobj = v
348-
# _key = "{key}{separator}{k}".format(key=key,separator=separator,k=k) if key != None else "{k}".format(k=k)
349-
350-
# __convert_Jobj_to_Jflat(_Jobj, _key)
351-
# elif type(Jobj) == list:
352-
# if len(Jobj) == 0:
353-
# Jflat[key] = list()
354-
# else:
355-
# for i,v in enumerate(Jobj):
356-
# _Jobj = v
357-
# _key = "{key}{separator}{parse_index}{i}{parse_index}".format(key=key, separator=separator, parse_index=parse_index, i=i) if key != None else "{parse_index}{i}{parse_index}".format(parse_index=parse_index, i=i)
334+
if type(self.getObject()) in [list, dict, OrderedDict]:
335+
if len(self.getObject()) > 0:
336+
try:
337+
Jflat = dict()
338+
if ordered_dict:
339+
Jflat = OrderedDict()
340+
341+
def __convert_Jobj_to_Jflat(Jobj, key=None):
342+
if type(Jobj) in [dict, OrderedDict]:
343+
if len(Jobj) == 0:
344+
Jflat[key] = dict()
345+
if ordered_dict:
346+
Jflat[key] = OrderedDict()
347+
else:
348+
for k,v in Jobj.items():
349+
_Jobj = v
350+
_key = "{key}{separator}{k}".format(key=key,separator=separator,k=k) if key != None else "{k}".format(k=k)
351+
352+
__convert_Jobj_to_Jflat(_Jobj, _key)
353+
elif type(Jobj) == list:
354+
if len(Jobj) == 0:
355+
Jflat[key] = list()
356+
else:
357+
for i,v in enumerate(Jobj):
358+
_Jobj = v
359+
_key = "{key}{separator}{parse_index}{i}{parse_index}".format(key=key, separator=separator, parse_index=parse_index, i=i) if key != None else "{parse_index}{i}{parse_index}".format(parse_index=parse_index, i=i)
358360

359-
# __convert_Jobj_to_Jflat(_Jobj, _key)
360-
# else:
361-
# Jflat[key] = Jobj
361+
__convert_Jobj_to_Jflat(_Jobj, _key)
362+
else:
363+
Jflat[key] = Jobj
362364

363365

364-
# try:
365-
# if type(separator) not in [str, unicode] or len(separator) == 0: separator = "||"
366-
# except Exception as e:
367-
# if type(separator) != str or len(separator) == 0: separator = "||"
366+
try:
367+
if type(separator) not in [str, unicode] or len(separator) == 0: separator = "||"
368+
except Exception as e:
369+
if type(separator) != str or len(separator) == 0: separator = "||"
368370

369-
# try:
370-
# if type(parse_index) not in [str, unicode] or len(parse_index) == 0: parse_index = "$"
371-
# except Exception as e:
372-
# if type(parse_index) != str or len(parse_index) == 0: parse_index = "$"
371+
try:
372+
if type(parse_index) not in [str, unicode] or len(parse_index) == 0: parse_index = "$"
373+
except Exception as e:
374+
if type(parse_index) != str or len(parse_index) == 0: parse_index = "$"
373375

374-
# __convert_Jobj_to_Jflat(self.__Jobj)
376+
__convert_Jobj_to_Jflat(self.getObject())
375377

376-
# self.__Jobj = Jflat
377-
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
378-
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
379-
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
378+
self.__Jobj = Jflat
379+
except Exception as e:
380+
if _isDebug_: print("\x1b[31m[-] ExceptionError: {}\x1b[0m".format(e))
381+
else:
382+
if _isDebug_: print("\x1b[31m[-] DataTypeError: the JSON object must be list, dict or OrderedDict, not {}\x1b[0m".format(type(self.getObject())))
383+
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
384+
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
385+
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
380386

381387

382388
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
383389
# # # # # # # # # # # # # # unflatten # # # # # # # # # # # # #
384390
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
385-
# def unflatten(self, separator="||", parse_index="$", ordered_dict=False):
391+
# def unflatten(self, separator="||", parse_index="$", ordered_dict=False, _isDebug_=True):
386392
# import re
387393
# from collections import OrderedDict
388394

0 commit comments

Comments
 (0)