json-duplicate-keys 2024.11.19__tar.gz → 2024.11.28__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,415 @@
1
+ Metadata-Version: 2.1
2
+ Name: json-duplicate-keys
3
+ Version: 2024.11.28
4
+ Summary: Flatten/ Unflatten and Load(s)/ Dump(s) JSON File/ Object with Duplicate Keys
5
+ Home-page: https://github.com/truocphan/json-duplicate-keys
6
+ Author: TP Cyber Security
7
+ Author-email: tpcybersec2023@gmail.com
8
+ License: MIT
9
+ Keywords: TPCyberSec,json,duplicate keys,json duplicate keys,flatten,unflatten
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Programming Language :: Python :: 2
12
+ Classifier: Programming Language :: Python :: Implementation :: Jython
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+
16
+ # JSON Duplicate Keys - PyPI
17
+ Flatten/ Unflatten and Load(s)/ Dump(s) JSON File/ Object with Duplicate Keys
18
+
19
+ <p align="center">
20
+ <a href="https://github.com/truocphan/json-duplicate-keys/releases/"><img src="https://img.shields.io/github/release/truocphan/json-duplicate-keys" height=30></a>
21
+ <a href="#"><img src="https://img.shields.io/github/downloads/truocphan/json-duplicate-keys/total" height=30></a>
22
+ <a href="#"><img src="https://img.shields.io/github/stars/truocphan/json-duplicate-keys" height=30></a>
23
+ <a href="#"><img src="https://img.shields.io/github/forks/truocphan/json-duplicate-keys" height=30></a>
24
+ <a href="https://github.com/truocphan/json-duplicate-keys/issues?q=is%3Aopen+is%3Aissue"><img src="https://img.shields.io/github/issues/truocphan/json-duplicate-keys" height=30></a>
25
+ <a href="https://github.com/truocphan/json-duplicate-keys/issues?q=is%3Aissue+is%3Aclosed"><img src="https://img.shields.io/github/issues-closed/truocphan/json-duplicate-keys" height=30></a>
26
+ </p>
27
+
28
+ ## Installation
29
+ #### From PyPI:
30
+ ```console
31
+ pip install json-duplicate-keys
32
+ ```
33
+ #### From Source:
34
+ ```console
35
+ git clone https://github.com/truocphan/json-duplicate-keys.git --branch <Branch/Tag>
36
+ cd json-duplicate-keys
37
+ python setup.py build
38
+ python setup.py install
39
+ ```
40
+
41
+ ## Basic Usage
42
+ ### normalize_key(`name`, `dupSign_start`="{{{", `dupSign_end`="}}}", `_isDebug_`=False)
43
+ _Normalize Key name_
44
+ - `name`: key name
45
+ - `dupSign_start`:
46
+ - `dupSign_end`:
47
+ - `_isDebug_`: Show/ Hide debug error messages
48
+ ```python
49
+ import json_duplicate_keys as jdks
50
+
51
+ print(jdks.normalize_key("version{{{_2_}}}"))
52
+ # OUTPUT: version
53
+ ```
54
+ ---
55
+
56
+ ### loads(`Jstr`, `dupSign_start`="{{{", `dupSign_end`="}}}", `ordered_dict`=False, `_isDebug_`=False)
57
+ _Deserialize a JSON format string to a class `JSON_DUPLICATE_KEYS`_
58
+ - `Jstr`: a JSON format string
59
+ - `dupSign_start`:
60
+ - `dupSign_end`:
61
+ - `ordered_dict`: preserves the order in which the Keys are inserted
62
+ - `_isDebug_`: Show/ Hide debug error messages
63
+ ```python
64
+ import json_duplicate_keys as jdks
65
+
66
+ Jstr = '{"author": "truocphan", "version": "22.3.3", "version": "latest", "release": [{"version": "latest"}], "snapshot": {"author": "truocphan", "version": "22.3.3", "release": [{"version": "latest"}]}}'
67
+
68
+ JDKSObject = jdks.loads(Jstr)
69
+
70
+ print(JDKSObject)
71
+ # OUTPUT: <json_duplicate_keys.JSON_DUPLICATE_KEYS object at 0x00000270AE987940>
72
+ ```
73
+ ---
74
+
75
+ ### load(`Jfilepath`, `dupSign_start`="{{{", `dupSign_end`="}}}", `ordered_dict`=False, `_isDebug_`=False)
76
+ _Deserialize a JSON format string from a file to a class `JSON_DUPLICATE_KEYS`_
77
+ - `Jfilepath`: The path to the file containing the JSON format string
78
+ - `dupSign_start`:
79
+ - `dupSign_end`:
80
+ - `ordered_dict`: preserves the order in which the Keys are inserted
81
+ - `_isDebug_`: Show/ Hide debug error messages
82
+ ```python
83
+ # /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"}]}}
84
+
85
+ import json_duplicate_keys as jdks
86
+
87
+ Jfilepath = "/path/to/file.json"
88
+
89
+ JDKSObject = jdks.load(Jfilepath)
90
+
91
+ print(JDKSObject)
92
+ # OUTPUT: <json_duplicate_keys.JSON_DUPLICATE_KEYS object at 0x00000270AE986D40>
93
+ ```
94
+ ---
95
+
96
+ ### JSON_DUPLICATE_KEYS.getObject()
97
+ _Get the JSON object_
98
+ ```python
99
+ import json_duplicate_keys as jdks
100
+
101
+ Jstr = '{"author": "truocphan", "version": "22.3.3", "version": "latest", "release": [{"version": "latest"}], "snapshot": {"author": "truocphan", "version": "22.3.3", "release": [{"version": "latest"}]}}'
102
+
103
+ JDKSObject = jdks.loads(Jstr)
104
+
105
+ print(JDKSObject.getObject())
106
+ # OUTPUT: {'author': 'truocphan', 'version': '22.3.3', 'version{{{_2_}}}': 'latest', 'release': [{'version': 'latest'}], 'snapshot': {'author': 'truocphan', 'version': '22.3.3', 'release': [{'version': 'latest'}]}}
107
+ ```
108
+ ---
109
+
110
+ ### JSON_DUPLICATE_KEYS.get(`name`, `case_insensitive`=False, `separator`="||", `parse_index`="$", `_isDebug_`=False)
111
+ _Get value in the JSON object by `name`_
112
+ - `name`: the key name of the JSON object. Supported flatten key name format
113
+ - `case_insensitive`: the key name case (in)sensitive
114
+ - `separator`:
115
+ - `parse_index`:
116
+ - `_isDebug_`: Show/ Hide debug error messages
117
+ ```python
118
+ import json_duplicate_keys as jdks
119
+
120
+ Jstr = '{"author": "truocphan", "version": "22.3.3", "version": "latest", "release": [{"version": "latest"}], "snapshot": {"author": "truocphan", "version": "22.3.3", "release": [{"version": "latest"}]}}'
121
+
122
+ JDKSObject = jdks.loads(Jstr)
123
+
124
+ print(JDKSObject.get("version{{{_2_}}}"))
125
+ # OUTPUT: latest
126
+
127
+ print(JDKSObject.get("release||$0$"))
128
+ # OUTPUT: {'version': 'latest'}
129
+
130
+ print(JDKSObject.get("snapshot||author"))
131
+ # OUTPUT: truocphan
132
+ ```
133
+ ---
134
+
135
+ ### JSON_DUPLICATE_KEYS.set(`name`, `value`, `case_insensitive`=False, `separator`="||", `parse_index`="$", `dupSign_start`="{{{", `dupSign_end`="}}}", `ordered_dict`=False, `_isDebug_`=False)
136
+ _Set a new `name` and `value` for the JSON object_
137
+ - `name`: new key name for the JSON object. Supported flat key name format
138
+ - `value`: value for key `name`
139
+ - `case_insensitive`: the key name case (in)sensitive
140
+ - `separator`:
141
+ - `parse_index`:
142
+ - `dupSign_start`:
143
+ - `dupSign_end`:
144
+ - `ordered_dict`: preserves the order in which the Keys are inserted
145
+ - `_isDebug_`: Show/Hide debug error messages
146
+ ```python
147
+ import json_duplicate_keys as jdks
148
+
149
+ Jstr = '{}'
150
+ JDKSObject = jdks.loads(Jstr)
151
+
152
+ print(JDKSObject.getObject())
153
+ # OUTPUT: {}
154
+
155
+ JDKSObject.set("author", "truocphan")
156
+ print(JDKSObject.getObject())
157
+ # OUTPUT: {'author': 'truocphan'}
158
+
159
+ JDKSObject.set("version", "22.3.3")
160
+ print(JDKSObject.getObject())
161
+ # OUTPUT: {'author': 'truocphan', 'version': '22.3.3'}
162
+
163
+ JDKSObject.set("version", "latest")
164
+ print(JDKSObject.getObject())
165
+ # OUTPUT: {'author': 'truocphan', 'version': '22.3.3', 'version{{{_2_}}}': 'latest'}
166
+
167
+ JDKSObject.set("release", [{"version": "latest"}])
168
+ print(JDKSObject.getObject())
169
+ # OUTPUT: {'author': 'truocphan', 'version': '22.3.3', 'version{{{_2_}}}': 'latest', 'release': [{'version': 'latest'}]}
170
+
171
+ JDKSObject.set("snapshot", {})
172
+ print(JDKSObject.getObject())
173
+ # OUTPUT: {'author': 'truocphan', 'version': '22.3.3', 'version{{{_2_}}}': 'latest', 'release': [{'version': 'latest'}], 'snapshot': {}}
174
+
175
+ JDKSObject.set("snapshot||author", "truocphan")
176
+ print(JDKSObject.getObject())
177
+ # OUTPUT: {'author': 'truocphan', 'version': '22.3.3', 'version{{{_2_}}}': 'latest', 'release': [{'version': 'latest'}], 'snapshot': {'author': 'truocphan'}}
178
+
179
+
180
+ Jstr = '[]'
181
+ JDKSObject = jdks.loads(Jstr)
182
+
183
+ print(JDKSObject.getObject())
184
+ # OUTPUT: []
185
+
186
+ JDKSObject.set("author", "truocphan")
187
+ print(JDKSObject.getObject())
188
+ # OUTPUT: [{'author': 'truocphan'}]
189
+
190
+ JDKSObject.set("release", [])
191
+ print(JDKSObject.getObject())
192
+ # OUTPUT: [{'author': 'truocphan'}, {'release': []}]
193
+
194
+ JDKSObject.set("$1$||release||", {"version": "latest"})
195
+ print(JDKSObject.getObject())
196
+ # OUTPUT: [{'author': 'truocphan'}, {'release': [{'version': 'latest'}]}]
197
+ ```
198
+ ---
199
+
200
+ ### JSON_DUPLICATE_KEYS.update(`name`, `value`, `case_insensitive`=False, `allow_new_key`=False, `separator`="||", `parse_index`="$", `dupSign_start`="{{{", `dupSign_end`="}}}", `ordered_dict`=False, `_isDebug_`=False)
201
+ _Update new `value` for existing `name` or Set a new `name` in the JSON object_
202
+ - `name`: the key name of the JSON object. Supported flatten key name format
203
+ - `value`: new value for key `name`
204
+ - `case_insensitive`: the key name case (in)sensitive
205
+ - `allow_new_key`: allows to create a new key name if the key name does not exist
206
+ - `separator`:
207
+ - `parse_index`:
208
+ - `dupSign_start`:
209
+ - `dupSign_end`:
210
+ - `ordered_dict`: preserves the order in which the Keys are inserted
211
+ - `_isDebug_`: Show/ Hide debug error messages
212
+ ```python
213
+ import json_duplicate_keys as jdks
214
+
215
+ Jstr = '{"author": "truocphan", "version": "22.3.3", "version": "latest", "release": [{"version": "latest"}], "snapshot": {"author": "truocphan", "version": "22.3.3", "release": [{"version": "latest"}]}}'
216
+
217
+ JDKSObject = jdks.loads(Jstr)
218
+
219
+ print(JDKSObject.getObject())
220
+ # OUTPUT: {'author': 'truocphan', 'version': '22.3.3', 'version{{{_2_}}}': 'latest', 'release': [{'version': 'latest'}], 'snapshot': {'author': 'truocphan', 'version': '22.3.3', 'release': [{'version': 'latest'}]}}
221
+
222
+ JDKSObject.update("version{{{_2_}}}", ["22.3.3", "latest"])
223
+ JDKSObject.update("snapshot||version", "latest")
224
+
225
+ print(JDKSObject.getObject())
226
+ # OUTPUT: {'author': 'truocphan', 'version': '22.3.3', 'version{{{_2_}}}': ['22.3.3', 'latest'], 'release': [{'version': 'latest'}], 'snapshot': {'author': 'truocphan', 'version': 'latest', 'release': [{'version': 'latest'}]}}
227
+ ```
228
+ ---
229
+
230
+ ### JSON_DUPLICATE_KEYS.delete(`name`, `case_insensitive`=False, `separator`="||", `parse_index`="$", `_isDebug_`=False)
231
+ _Delete a key-value pair in a JSON object by key `name`_
232
+ - `name`: the key name of the JSON object. Supported flatten key name format
233
+ - `case_insensitive`: the key name case (in)sensitive
234
+ - `separator`:
235
+ - `parse_index`:
236
+ - `_isDebug_`: Show/ Hide debug error messages
237
+ ```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.delete("version")
248
+ JDKSObject.delete("release||$0$")
249
+ JDKSObject.delete("snapshot")
250
+
251
+ print(JDKSObject.getObject())
252
+ # OUTPUT: {'author': 'truocphan', 'version{{{_2_}}}': 'latest', 'release': []}
253
+ ```
254
+ ---
255
+
256
+ ### JSON_DUPLICATE_KEYS.filter_keys(`name`, `separator`="||", `parse_index`="$", `ordered_dict`=False)
257
+
258
+ - `name`:
259
+ - `separator`:
260
+ - `parse_index`:
261
+ - `ordered_dict`: preserves the order in which the Keys are inserted
262
+ ```python
263
+ import json_duplicate_keys as jdks
264
+
265
+ Jstr = '{"author": "truocphan", "version": "22.3.3", "version": "latest", "release": [{"version": "latest"}], "snapshot": {"author": "truocphan", "version": "22.3.3", "release": [{"version": "latest"}]}}'
266
+
267
+ JDKSObject = jdks.loads(Jstr)
268
+
269
+ print(JDKSObject.filter_keys("version").dumps())
270
+ # OUTPUT: {"version": "22.3.3", "version": "latest", "release||$0$||version": "latest", "snapshot||version": "22.3.3", "snapshot||release||$0$||version": "latest"}
271
+
272
+ print(JDKSObject.dumps())
273
+ # OUTPUT: {"author": "truocphan", "version": "22.3.3", "version": "latest", "release": [{"version": "latest"}], "snapshot": {"author": "truocphan", "version": "22.3.3", "release": [{"version": "latest"}]}}
274
+ ```
275
+ ---
276
+
277
+ ### JSON_DUPLICATE_KEYS.filter_values(`value`, `separator`="||", `parse_index`="$", `ordered_dict`=False)
278
+
279
+ - `value`:
280
+ - `separator`:
281
+ - `parse_index`:
282
+ - `ordered_dict`: preserves the order in which the Keys are inserted
283
+ ```python
284
+ import json_duplicate_keys as jdks
285
+
286
+ Jstr = '{"author": "truocphan", "version": "22.3.3", "version": "latest", "release": [{"version": "latest"}], "snapshot": {"author": "truocphan", "version": "22.3.3", "release": [{"version": "latest"}]}}'
287
+
288
+ JDKSObject = jdks.loads(Jstr)
289
+
290
+ print(JDKSObject.filter_values("latest").dumps())
291
+ # OUTPUT: {"version": "latest", "release||$0$||version": "latest", "snapshot||release||$0$||version": "latest"}
292
+
293
+ print(JDKSObject.dumps())
294
+ # OUTPUT: {"author": "truocphan", "version": "22.3.3", "version": "latest", "release": [{"version": "latest"}], "snapshot": {"author": "truocphan", "version": "22.3.3", "release": [{"version": "latest"}]}}
295
+ ```
296
+ ---
297
+
298
+ ### 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)
299
+ _Serialize a JSON object to a JSON format string_
300
+ - `dupSign_start`:
301
+ - `dupSign_end`:
302
+ - `_isDebug_`: Show/ Hide debug error messages
303
+ - For remaining arguments, please refer to [json.dump()](https://docs.python.org/3/library/json.html#json.dump)
304
+ ```python
305
+ import json_duplicate_keys as jdks
306
+
307
+ Jstr = '{"author": "truocphan", "version": "22.3.3", "version": "latest", "release": [{"version": "latest"}], "snapshot": {"author": "truocphan", "version": "22.3.3", "release": [{"version": "latest"}]}}'
308
+
309
+ JDKSObject = jdks.loads(Jstr)
310
+
311
+ print(JDKSObject.getObject())
312
+ # OUTPUT: {'author': 'truocphan', 'version': '22.3.3', 'version{{{_2_}}}': 'latest', 'release': [{'version': 'latest'}], 'snapshot': {'author': 'truocphan', 'version': '22.3.3', 'release': [{'version': 'latest'}]}}
313
+
314
+ JDKSObject.delete("version")
315
+ JDKSObject.delete("release||$0$")
316
+ JDKSObject.delete("snapshot")
317
+
318
+ print(JDKSObject.dumps())
319
+ # OUTPUT: {"author": "truocphan", "version": "latest", "release": []}
320
+ ```
321
+ ---
322
+
323
+ ### 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)
324
+ _Serialize a JSON object to a JSON format string and write to a file_
325
+ - `Jfilepath`: the path to the file to save the JSON format string
326
+ - `dupSign_start`:
327
+ - `dupSign_end`:
328
+ - `_isDebug_`: Show/ Hide debug error messages
329
+ - For remaining arguments, please refer to [json.dump()](https://docs.python.org/3/library/json.html#json.dump)
330
+ ```python
331
+ import json_duplicate_keys as jdks
332
+
333
+ Jstr = '{"author": "truocphan", "version": "22.3.3", "version": "latest", "release": [{"version": "latest"}], "snapshot": {"author": "truocphan", "version": "22.3.3", "release": [{"version": "latest"}]}}'
334
+
335
+ JDKSObject = jdks.loads(Jstr)
336
+
337
+ print(JDKSObject.getObject())
338
+ # OUTPUT: {'author': 'truocphan', 'version': '22.3.3', 'version{{{_2_}}}': 'latest', 'release': [{'version': 'latest'}], 'snapshot': {'author': 'truocphan', 'version': '22.3.3', 'release': [{'version': 'latest'}]}}
339
+
340
+ JDKSObject.delete("version")
341
+ JDKSObject.delete("release||$0$")
342
+ JDKSObject.delete("snapshot")
343
+
344
+ Jfilepath = "/path/to/file.json"
345
+ JDKSObject.dump(Jfilepath)
346
+
347
+ JDKSObject_load = jdks.load(Jfilepath)
348
+ print(JDKSObject_load.getObject())
349
+ # OUTPUT: {'author': 'truocphan', 'version': 'latest', 'release': []}
350
+ ```
351
+ ---
352
+
353
+ ### JSON_DUPLICATE_KEYS.flatten(`separator`="||", `parse_index`="$", `ordered_dict`=False, `_isDebug_`=False)
354
+ _Flatten a JSON object to a single key-value pairs_
355
+ - `separator`:
356
+ - `parse_index`:
357
+ - `ordered_dict`: preserves the order in which the Keys are inserted
358
+ - `_isDebug_`: Show/ Hide debug error messages
359
+ ```python
360
+ import json_duplicate_keys as jdks
361
+
362
+ Jstr = '{"author": "truocphan", "version": "22.3.3", "version": "latest", "release": [{"version": "latest"}], "snapshot": {"author": "truocphan", "version": "22.3.3", "release": [{"version": "latest"}]}}'
363
+
364
+ JDKSObject = jdks.loads(Jstr)
365
+
366
+ print(JDKSObject.getObject())
367
+ # OUTPUT: {'author': 'truocphan', 'version': '22.3.3', 'version{{{_2_}}}': 'latest', 'release': [{'version': 'latest'}], 'snapshot': {'author': 'truocphan', 'version': '22.3.3', 'release': [{'version': 'latest'}]}}
368
+
369
+ JDKSObject.flatten()
370
+
371
+ print(JDKSObject.getObject())
372
+ # 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'}
373
+ ```
374
+ ---
375
+
376
+ ### JSON_DUPLICATE_KEYS.unflatten(`separator`="||", `parse_index`="$", `ordered_dict`=False, `_isDebug_`=False)
377
+ _Unflatten a flattened JSON object back to a JSON object_
378
+ - `separator`:
379
+ - `parse_index`:
380
+ - `ordered_dict`: preserves the order in which the Keys are inserted
381
+ - `_isDebug_`: Show/ Hide debug error messages
382
+ ```python
383
+ import json_duplicate_keys as jdks
384
+
385
+ Jstr = '{"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"}'
386
+
387
+ JDKSObject = jdks.loads(Jstr)
388
+
389
+ print(JDKSObject.getObject())
390
+ # 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'}
391
+
392
+ JDKSObject.unflatten()
393
+
394
+ print(JDKSObject.getObject())
395
+ # OUTPUT: {'author': 'truocphan', 'version': '22.3.3', 'version{{{_2_}}}': 'latest', 'release': [{'version': 'latest'}], 'snapshot': {'author': 'truocphan', 'version': '22.3.3', 'release': [{'version': 'latest'}]}}
396
+ ```
397
+ ---
398
+
399
+ ## CHANGELOG
400
+ #### [json-duplicate-keys v2024.11.28](https://github.com/truocphan/json-duplicate-keys/tree/2024.11.28)
401
+ - **Fixed**: Add subkey name to empty dict of existing key name
402
+
403
+ #### [json-duplicate-keys v2024.11.19](https://github.com/truocphan/json-duplicate-keys/tree/2024.11.19)
404
+ - **Updated**: Allows getting (`JSON_DUPLICATE_KEYS.get`), setting (`JSON_DUPLICATE_KEYS.set`), updating (`JSON_DUPLICATE_KEYS.update`), deleting (`JSON_DUPLICATE_KEYS.delete`) JSON_DUPLICATE_KEYS objects with case-insensitive key names
405
+
406
+ #### [json-duplicate-keys v2024.7.17](https://github.com/truocphan/json-duplicate-keys/tree/2024.7.17)
407
+ - **Fixed**: issue [#3](https://github.com/truocphan/json-duplicate-keys/issues/3) break the set function when the key's value is empty. Thanks [ptth222](https://github.com/ptth222) for reporting this issue.
408
+
409
+ #### [json-duplicate-keys v2024.4.20](https://github.com/truocphan/json-duplicate-keys/tree/2024.4.20)
410
+ - **New**: _filter_values_
411
+ - **Updated**: _filter_keys_
412
+
413
+ #### [json-duplicate-keys v2024.3.24](https://github.com/truocphan/json-duplicate-keys/tree/2024.3.24)
414
+ - **Updated**: _normalize_key_, _loads_, _get_, _set_, _update_, _delete_
415
+ ---
@@ -182,13 +182,17 @@ print(JDKSObject.getObject())
182
182
  ```
183
183
  ---
184
184
 
185
- ### JSON_DUPLICATE_KEYS.update(`name`, `value`, `case_insensitive`=False, `separator`="||", `parse_index`="$", `_isDebug_`=False)
186
- _Update new `value` for existing `name` in the JSON object_
185
+ ### JSON_DUPLICATE_KEYS.update(`name`, `value`, `case_insensitive`=False, `allow_new_key`=False, `separator`="||", `parse_index`="$", `dupSign_start`="{{{", `dupSign_end`="}}}", `ordered_dict`=False, `_isDebug_`=False)
186
+ _Update new `value` for existing `name` or Set a new `name` in the JSON object_
187
187
  - `name`: the key name of the JSON object. Supported flatten key name format
188
188
  - `value`: new value for key `name`
189
189
  - `case_insensitive`: the key name case (in)sensitive
190
+ - `allow_new_key`: allows to create a new key name if the key name does not exist
190
191
  - `separator`:
191
192
  - `parse_index`:
193
+ - `dupSign_start`:
194
+ - `dupSign_end`:
195
+ - `ordered_dict`: preserves the order in which the Keys are inserted
192
196
  - `_isDebug_`: Show/ Hide debug error messages
193
197
  ```python
194
198
  import json_duplicate_keys as jdks
@@ -239,7 +243,7 @@ print(JDKSObject.getObject())
239
243
  - `name`:
240
244
  - `separator`:
241
245
  - `parse_index`:
242
- - `ordered_dict`:
246
+ - `ordered_dict`: preserves the order in which the Keys are inserted
243
247
  ```python
244
248
  import json_duplicate_keys as jdks
245
249
 
@@ -260,7 +264,7 @@ print(JDKSObject.dumps())
260
264
  - `value`:
261
265
  - `separator`:
262
266
  - `parse_index`:
263
- - `ordered_dict`:
267
+ - `ordered_dict`: preserves the order in which the Keys are inserted
264
268
  ```python
265
269
  import json_duplicate_keys as jdks
266
270
 
@@ -378,6 +382,9 @@ print(JDKSObject.getObject())
378
382
  ---
379
383
 
380
384
  ## CHANGELOG
385
+ #### [json-duplicate-keys v2024.11.28](https://github.com/truocphan/json-duplicate-keys/tree/2024.11.28)
386
+ - **Fixed**: Add subkey name to empty dict of existing key name
387
+
381
388
  #### [json-duplicate-keys v2024.11.19](https://github.com/truocphan/json-duplicate-keys/tree/2024.11.19)
382
389
  - **Updated**: Allows getting (`JSON_DUPLICATE_KEYS.get`), setting (`JSON_DUPLICATE_KEYS.set`), updating (`JSON_DUPLICATE_KEYS.update`), deleting (`JSON_DUPLICATE_KEYS.delete`) JSON_DUPLICATE_KEYS objects with case-insensitive key names
383
390
 
@@ -1,3 +1,4 @@
1
+ # -*- coding: utf-8 -*-
1
2
  try:
2
3
  unicode # Python 2
3
4
  except NameError:
@@ -150,9 +151,8 @@ def loads(Jstr, dupSign_start="{{{", dupSign_end="}}}", ordered_dict=False, _isD
150
151
  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
151
152
  def load(Jfilepath, dupSign_start="{{{", dupSign_end="}}}", ordered_dict=False, _isDebug_=False):
152
153
  try:
153
- Jfile = open(Jfilepath)
154
- Jstr = Jfile.read()
155
- Jfile.close()
154
+ with open(Jfilepath) as Jfile:
155
+ Jstr = Jfile.read()
156
156
 
157
157
  return loads(Jstr, dupSign_start=dupSign_start, dupSign_end=dupSign_end, ordered_dict=ordered_dict, _isDebug_=_isDebug_)
158
158
  except Exception as e:
@@ -307,16 +307,20 @@ class JSON_DUPLICATE_KEYS:
307
307
  else:
308
308
  if self.get(separator.join(name.split(separator)[:-1]), case_insensitive=case_insensitive, separator=separator, parse_index=parse_index)["value"] != "JSON_DUPLICATE_KEYS_ERROR":
309
309
  Jget = self.get(separator.join(name.split(separator)[:-1]), case_insensitive=case_insensitive, separator=separator, parse_index=parse_index)
310
- exec_expression = "self.getObject()"
310
+ if type(Jget["value"]) in [dict, OrderedDict]:
311
+ exec_expression = "self.getObject()"
311
312
 
312
- for k in Jget["name"].split(separator):
313
- if re.search("^"+re.escape(parse_index)+"\\d+"+re.escape(parse_index)+"$", k):
314
- exec_expression += "["+k.split(parse_index)[1]+"]"
315
- else:
316
- exec_expression += "["+repr(k)+"]"
313
+ for k in Jget["name"].split(separator)+[name.split(separator)[-1]]:
314
+ if re.search("^"+re.escape(parse_index)+"\\d+"+re.escape(parse_index)+"$", k):
315
+ exec_expression += "["+k.split(parse_index)[1]+"]"
316
+ else:
317
+ exec_expression += "["+repr(k)+"]"
317
318
 
318
- exec(exec_expression+"="+repr(value))
319
- return True
319
+ exec(exec_expression+"="+repr(value))
320
+ return True
321
+ else:
322
+ if _isDebug_: print("\x1b[31m[-] KeyNameInvalidError: \x1b[0m"+name)
323
+ return False
320
324
  else:
321
325
  if _isDebug_: print("\x1b[31m[-] KeyNameNotExistError: {}\x1b[0m".format(separator.join(Jget["name"].split(separator)[:-1])))
322
326
  return False
@@ -328,7 +332,7 @@ class JSON_DUPLICATE_KEYS:
328
332
  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
329
333
  # # # # # # # # # # # # # # update # # # # # # # # # # # # # #
330
334
  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
331
- def update(self, name, value, case_insensitive=False, separator="||", parse_index="$", _isDebug_=False):
335
+ def update(self, name, value, case_insensitive=False, allow_new_key=False, separator="||", parse_index="$", dupSign_start="{{{", dupSign_end="}}}", ordered_dict=False, _isDebug_=False):
332
336
  import re
333
337
 
334
338
  # User input data type validation
@@ -344,7 +348,10 @@ class JSON_DUPLICATE_KEYS:
344
348
 
345
349
  if type(parse_index) not in [str, unicode]: parse_index = "$"
346
350
 
347
- if self.get(name, case_insensitive=case_insensitive, separator=separator, parse_index=parse_index, _isDebug_=_isDebug_)["value"] != "JSON_DUPLICATE_KEYS_ERROR":
351
+ _debug_ = _isDebug_
352
+ if allow_new_key: _debug_ = False
353
+
354
+ if self.get(name, case_insensitive=case_insensitive, separator=separator, parse_index=parse_index, _isDebug_=_debug_)["value"] != "JSON_DUPLICATE_KEYS_ERROR":
348
355
  Jname = self.get(name, case_insensitive=case_insensitive, separator=separator, parse_index=parse_index)["name"]
349
356
  try:
350
357
  exec_expression = "self.getObject()"
@@ -359,6 +366,8 @@ class JSON_DUPLICATE_KEYS:
359
366
  return True
360
367
  except Exception as e:
361
368
  if _isDebug_: print("\x1b[31m[-] ExceptionError: {}\x1b[0m".format(e))
369
+ elif allow_new_key:
370
+ return self.set(name, value, case_insensitive=case_insensitive, separator=separator, parse_index=parse_index, dupSign_start=dupSign_start, dupSign_end=dupSign_end, ordered_dict=ordered_dict, _isDebug_=_isDebug_)
362
371
 
363
372
  return False
364
373
  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
@@ -420,10 +429,10 @@ class JSON_DUPLICATE_KEYS:
420
429
  for k, v in JDKSObject.getObject().items():
421
430
  if type(k) == str and type(name) == str:
422
431
  if re.search(name, k):
423
- newJDKSObject.set(k, v, separator="!!"+separator+"!!", parse_index="!!"+parse_index+"!!", ordered_dict=ordered_dict)
432
+ newJDKSObject.set(k, v, separator="§§"+separator+"§§", parse_index="§§"+parse_index+"§§", ordered_dict=ordered_dict)
424
433
  else:
425
434
  if name == k:
426
- newJDKSObject.set(k, v, separator="!!"+separator+"!!", parse_index="!!"+parse_index+"!!", ordered_dict=ordered_dict)
435
+ newJDKSObject.set(k, v, separator="§§"+separator+"§§", parse_index="§§"+parse_index+"§§", ordered_dict=ordered_dict)
427
436
 
428
437
  return newJDKSObject
429
438
  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
@@ -444,10 +453,10 @@ class JSON_DUPLICATE_KEYS:
444
453
  for k, v in JDKSObject.getObject().items():
445
454
  if type(v) == str and type(value) == str:
446
455
  if re.search(value, v):
447
- newJDKSObject.set(k, v, separator="!!"+separator+"!!", parse_index="!!"+parse_index+"!!", ordered_dict=ordered_dict)
456
+ newJDKSObject.set(k, v, separator="§§"+separator+"§§", parse_index="§§"+parse_index+"§§", ordered_dict=ordered_dict)
448
457
  else:
449
458
  if value == v:
450
- newJDKSObject.set(k, v, separator="!!"+separator+"!!", parse_index="!!"+parse_index+"!!", ordered_dict=ordered_dict)
459
+ newJDKSObject.set(k, v, separator="§§"+separator+"§§", parse_index="§§"+parse_index+"§§", ordered_dict=ordered_dict)
451
460
 
452
461
  return newJDKSObject
453
462
  # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #