Modify nested dict key name by its parent - python

I have a following kind of structure to be handled:
payload = {
"name":"Event1",
"events":[
{
"name":"A",
"data":[
{
"name":"subscriptionId",
"data_id":0,
"data":0
},
{
"name":"updateCounter",
"data_id":1,
"data":0
},
{
"name":"noOfMessages",
"data_id":2,
"data":0
},
{
"name":"counter",
"data_id":3,
"data":0
},
{
"name":"resourceElements",
"data_id":4,
"data":0
},
{
"name":"type",
"data_id":5,
"data":0
},
{
"name":"subscription",
"data_id":6,
"data":0
},
{
"name":"element",
"data_id":7,
"data":[
{
"name":"type",
"data_id":0,
"data":0
},
{
"name":"plugLockState",
"data_id":1,
"data":{
"value":""
}
},
{
"name":"lockState",
"data_id":2,
"data":{
"value":""
}
},
{
"name":"flapState",
"data_id":6,
"data":{
"value":""
}
},
{
"name":"plugState",
"data_id":3,
"data":0
},
{
"name":"plugConnectionState",
"data_id":4,
"data":0
},
{
"name":"infrastructureState",
"data_id":5,
"data":0
}
]
}
]
}
]
}
I want to replace any key name within the nested structure by the parent, so the ideal result should look like this:
{
"name":"Event1",
"events":[
{
"name":"Event1.A",
"data":[
{
"name":"Event1.A.subscriptionId",
"data_id":0,
"data":0
},
{
"name":"Event1.A.updateCounter",
"data_id":1,
"data":0
},
{
"name":"Event1.A.noOfMessages",
"data_id":2,
"data":0
},
{
"name":"Event1.A.counter",
"data_id":3,
"data":0
},
{
"name":"Event1.A.resourceElements",
"data_id":4,
"data":0
},
{
"name":"Event1.A.type",
"data_id":5,
"data":0
},
{
"name":"Event1.A.subscription",
"data_id":6,
"data":0
},
{
"name":"Event1.A.element",
"data_id":7,
"data":[
{
"name":"Event1.A.element.type",
"data_id":0,
"data":0
},
{
"name":"Event1.A.element.plugLockState",
"data_id":1,
"data":{
"value":""
}
},
{
"name":"Event1.A.element.lockState",
"data_id":2,
"data":{
"value":""
}
},
{
"name":"Event1.A.element.flapState",
"data_id":6,
"data":{
"value":""
}
},
{
"name":"Event1.A.element.plugState",
"data_id":3,
"data":0
},
{
"name":"Event1.A.element.plugConnectionState",
"data_id":4,
"data":0
},
{
"name":"Event1.A.element.infrastructureState",
"data_id":5,
"data":0
}
]
}
]
}
]
}
so far I have written this recursive method:
def iterate_recursively(dictionary: dict, names=None):
if names is None:
names = []
for k, v in dictionary.items():
if isinstance(v, dict):
iterate_recursively(v)
elif isinstance(v, list):
for d in v:
if isinstance(d, dict):
names.append(d["name"])
iterate_recursively(d)
but I simply don't get it. How can the keys, based on my requirement, be changed while iterating recursively?

Here's a variant that returns a new dictionary (and thus leaving the original one unchanged).
code00.py:
#!/usr/bin/env python
import sys
from pprint import pprint as pp
payload = {
"name": "Event1",
"events": [
{
"name": "A",
"data": [
{
"name": "subscriptionId",
"data_id": 0,
"data": 0
},
{
"name": "updateCounter",
"data_id": 1,
"data": 0
},
{
"name": "noOfMessages",
"data_id": 2,
"data": 0
},
{
"name": "counter",
"data_id": 3,
"data": 0
},
{
"name": "resourceElements",
"data_id": 4,
"data": 0
},
{
"name": "type",
"data_id": 5,
"data": 0
},
{
"name": "subscription",
"data_id": 6,
"data": 0
},
{
"name": "element",
"data_id": 7,
"data": [
{
"name": "type",
"data_id": 0,
"data": 0
},
{
"name": "plugLockState",
"data_id": 1,
"data": {
"value": ""
}
},
{
"name": "lockState",
"data_id": 2,
"data": {
"value": ""
}
},
{
"name": "flapState",
"data_id": 6,
"data": {
"value": ""
}
},
{
"name": "plugState",
"data_id": 3,
"data": 0
},
{
"name": "plugConnectionState",
"data_id": 4,
"data": 0
},
{
"name": "infrastructureState",
"data_id": 5,
"data": 0
}
]
}
]
}
]
}
def concat_names(data, names=()):
if isinstance(data, dict):
name = data.get("name")
new_names = names + (name,) if name is not None else names
return {k: concat_names(v, names=new_names) if k != "name" else ".".join(new_names) for k, v in data.items()}
elif isinstance(data, (list, tuple)):
return [concat_names(e, names=names) for e in data]
else:
return data
def main(*argv):
pp(concat_names(payload), indent=2, sort_dicts=False)
if __name__ == "__main__":
print("Python {:s} {:03d}bit on {:s}\n".format(" ".join(elem.strip() for elem in sys.version.split("\n")),
64 if sys.maxsize > 0x100000000 else 32, sys.platform))
rc = main(*sys.argv[1:])
print("\nDone.")
sys.exit(rc)
Output:
[cfati#CFATI-5510-0:e:\Work\Dev\StackOverflow\q073621243]> "e:\Work\Dev\VEnvs\py_pc064_03.09_test0\Scripts\python.exe" ./code00.py
Python 3.9.9 (tags/v3.9.9:ccb0e6a, Nov 15 2021, 18:08:50) [MSC v.1929 64 bit (AMD64)] 064bit on win32
{ 'name': 'Event1',
'events': [ { 'name': 'Event1.A',
'data': [ { 'name': 'Event1.A.subscriptionId',
'data_id': 0,
'data': 0},
{ 'name': 'Event1.A.updateCounter',
'data_id': 1,
'data': 0},
{ 'name': 'Event1.A.noOfMessages',
'data_id': 2,
'data': 0},
{'name': 'Event1.A.counter', 'data_id': 3, 'data': 0},
{ 'name': 'Event1.A.resourceElements',
'data_id': 4,
'data': 0},
{'name': 'Event1.A.type', 'data_id': 5, 'data': 0},
{ 'name': 'Event1.A.subscription',
'data_id': 6,
'data': 0},
{ 'name': 'Event1.A.element',
'data_id': 7,
'data': [ { 'name': 'Event1.A.element.type',
'data_id': 0,
'data': 0},
{ 'name': 'Event1.A.element.plugLockState',
'data_id': 1,
'data': {'value': ''}},
{ 'name': 'Event1.A.element.lockState',
'data_id': 2,
'data': {'value': ''}},
{ 'name': 'Event1.A.element.flapState',
'data_id': 6,
'data': {'value': ''}},
{ 'name': 'Event1.A.element.plugState',
'data_id': 3,
'data': 0},
{ 'name': 'Event1.A.element.plugConnectionState',
'data_id': 4,
'data': 0},
{ 'name': 'Event1.A.element.infrastructureState',
'data_id': 5,
'data': 0}]}]}]}
Done.

You can do something like this:
def iterate_recursively(dictionary: dict, prefix_name=None):
if 'name' in dictionary:
if prefix_name is None:
prefix_name = dictionary['name']
else:
prefix_name += '.' + dictionary['name']
dictionary['name'] = prefix_name
for k, v in dictionary.items():
if isinstance(v, dict):
iterate_recursively(v, prefix_name)
elif isinstance(v, list):
for d in v:
iterate_recursively(d, prefix_name)

Related

How to convert the following data structure to a hierarchical data structure

There is an array, each element in the array is a node in the following figure, and there is a hierarchical relationship between the nodes, similar to the "tree" data structure (except that the child node can refer back to the parent node).
#The current data structure is in the following format
[
{
'id': 1,
'name': '开始',
'next': '2,3,4'
},
{
'id': 2,
'name': '2号',
'next': ''
},
{
'id': 3,
'name': '3号',
'next': '5,8'
},
{
'id': 4,
'name': '4号',
'next': '6'
},
{
'id': 5,
'name': '5号',
'next': '7'
},
{
'id': 6,
'name': '6号',
'next': ''
},
{
'id': 7,
'name': '7号',
'next': '1,3,5'
},
{
'id': 8,
'name': '8号',
'next': ''
}
]
In the case of ensuring that the first element in the above array is the root node, please write a code to convert any of the above types of data formats into the following hierarchical format.
#to convert
{
"id":1,
"name":"开始",
"backpoints":[ ],
"childs":[
{
"id":2,
"name":"2号",
"backpoints":[ ],
"childs":[ ]
},
{
"id":3,
"name":"3号",
"backpoints":[ ],
"childs":[
{
"id":5,
"name":"5号",
"backpoints":[ ],
"childs":[
{
"id":7,
"name":"7号",
"backpoints":[
"1",
"3",
"5"
],
"childs":[ ]
}
]
},
{
"id":8,
"name":"8号",
"backpoints":[ ],
"childs":[ ]
}
]
},
{
"id":4,
"name":"4号",
"backpoints":[ ],
"childs":[
{
"id":6,
"name":"6号",
"backpoints":[ ],
"childs":[ ]
}
]
}
]
}
You can iterate over the given list of dicts (named nodes in the following example) and use a dict that maps node IDs to node objects, and iterate over the node IDs in the next key to pre-create entries in the mapping dict as items in the childs sub-list if the ID does not already exist in the mapping dict, or append the ID to the backpoints sub-list:
mapping = {}
for node in nodes:
nexts = node.pop('next')
entry = mapping.setdefault(node['id'], {})
entry.update({**node, **{'backpoints': [], 'childs': []}})
if nexts:
for n in map(int, nexts.split(',')):
if n in mapping:
entry['backpoints'].append(str(n))
else:
entry['childs'].append(mapping.setdefault(n, {}))
so that mapping[nodes[0]['id']] would return:
{
"id": 1,
"name": "开始",
"backpoints": [],
"childs": [
{
"id": 2,
"name": "2号",
"backpoints": [],
"childs": []
},
{
"id": 3,
"name": "3号",
"backpoints": [],
"childs": [
{
"id": 5,
"name": "5号",
"backpoints": [],
"childs": [
{
"id": 7,
"name": "7号",
"backpoints": [
"1",
"3",
"5"
],
"childs": []
}
]
},
{
"id": 8,
"name": "8号",
"backpoints": [],
"childs": []
}
]
},
{
"id": 4,
"name": "4号",
"backpoints": [],
"childs": [
{
"id": 6,
"name": "6号",
"backpoints": [],
"childs": []
}
]
}
]
}
Demo: https://repl.it/repls/StrikingFunctionalCrash

Remove duplicate values from list of nested dictionaries

I have list of dictionaries with nested structure. I need to remove all duplicate values. I'm newbie in Python and can't solve this task. Anyone can help me?
My list looks like:
[
{
"task_id":123,
"results":[
{
"url":"site.com",
"date":"04.18.2019"
},
{
"url":"another_site.com",
"date":"04.18.2019"
},
{
"url":"site1.com",
"date":"04.18.2019"
}
]
},
{
"task_id":456,
"results":[
{
"url":"site3.com",
"date":"04.18.2019"
},
{
"url":"site.com",
"date":"04.18.2019"
}
]
},
{
"task_id":789,
"results":[
{
"url":"site7.com",
"date":"04.18.2019"
},
{
"url":"site9.com",
"date":"04.18.2019"
},
{
"url":"site.com",
"date":"04.18.2019"
}
]
}
]
I need to set site.com only once. If any value of url is duplicated - exclude it from dict.
As result:
task 123 with 3 dicts in results
task 456 with 1 dict in results (exclude site.com)
task 789 with 2 dict in results (exclude site.com)
Desired output should looks like:
[
{
"task_id":123,
"results":[
{
"url":"site.com",
"date":"04.18.2019"
},
{
"url":"another_site.com",
"date":"04.18.2019"
},
{
"url":"site1.com",
"date":"04.18.2019"
}
]
},
{
"task_id":456,
"results":[
{
"url":"site3.com",
"date":"04.18.2019"
}
]
},
{
"task_id":789,
"results":[
{
"url":"site7.com",
"date":"04.18.2019"
},
{
"url":"site9.com",
"date":"04.18.2019"
}
]
}
]
let results to be your array.
u = set()
final = []
for dict in results:
for res in dict["results"]:
if res["url"] not in u:
u.add(res["url"])
final.append(res)
print(final)
You can use a list comprehension:
d = [{'task_id': 123, 'results': [{'url': 'site.com', 'date': '04.18.2019'}, {'url': 'another_site.com', 'date': '04.18.2019'}, {'url': 'site1.com', 'date': '04.18.2019'}]}, {'task_id': 456, 'results': [{'url': 'site3.com', 'date': '04.18.2019'}, {'url': 'site.com', 'date': '04.18.2019'}]}, {'task_id': 789, 'results': [{'url': 'site7.com', 'date': '04.18.2019'}, {'url': 'site9.com', 'date': '04.18.2019'}, {'url': 'site.com', 'date': '04.18.2019'}]}]
new_d = [{**a, 'results':[c for c in a['results'] if all(c not in b['results'] for b in d[:i])]} for i, a in enumerate(d)]
Output:
[
{
"task_id": 123,
"results": [
{
"url": "site.com",
"date": "04.18.2019"
},
{
"url": "another_site.com",
"date": "04.18.2019"
},
{
"url": "site1.com",
"date": "04.18.2019"
}
]
},
{
"task_id": 456,
"results": [
{
"url": "site3.com",
"date": "04.18.2019"
}
]
},
{
"task_id": 789,
"results": [
{
"url": "site7.com",
"date": "04.18.2019"
},
{
"url": "site9.com",
"date": "04.18.2019"
}
]
}
]
people = {
1: {'name': 'John',},
2: {'name': 'Marie'},
3: {'name': 'Ann',},
4: {'name': 'John'},
}
print(people)
unique = {}
for key, value in people.items():
if value not in unique.values():
unique[key] = value
print(unique)
try these

how to generate a user required JSON file

I have a JSON file. I would like to change to User Required Format.
input.json file
[{
"Data": [{
"name": [" Devlopment", "34876", "Tez", "4578"],
"results": [{
"sum": 54
}]
}, {
"name": ["production", "09876", "phonepay", "2312"],
"results": [{
"sum": 50.0
}]
}],
"totalResult": {
"results": [{
"sum": 2027.0
}]
},
"unknownGroup": {
"results": [{
"sum": 0.0
}]
},
"performanceStats": {
"fileReadCount": 1,
"decompressionCount": 0,
"decompressionCacheEnabledCount": 0
},
"metadata": {
"eventTypes": ["Data_collection"],
"eventType": "Data_collection",
"openEnded": true
}
},
{
"Data":
[{
"name": [" Quality_Analyst", "623456", "slicepay", "989766"],
"results": [{
"sum": 54
}]
}, {
"name": ["Testing", "7654", "krazybee", "1234"],
"results": [{
"sum": 50.0
}]
}],
"totalResult": {
"results": [{
"sum": 2027.0
}]
},
"unknownGroup": {
"results": [{
"sum": 0.0
}]
},
"performanceStats": {
"fileReadCount": 1,
"decompressionCount": 0,
"decompressionCacheEnabledCount": 0
},
"metadata": {
"eventTypes": ["Data_collection"],
"eventType": "Data_collection",
"openEnded": true
}
}]
I am expecting Output:
==============================
Generate a new JSON file with the help of python:
Read the given JSON file and make a new JSON file
[{
"Data": [{
"Domain_name": " Devlopment",
"Domain_Id": "34876",
"app": "Tez",
"appId": 4578,
"sum": 54
}, {
"Domain_name": "production",
"Domain_Id": "09876",
"app": "phonepay",
"appId": 2312,
"sum": 54
}],
"totalResult": {
"results": [{
"sum": 2027.0
}]
},
"unknownGroup": {
"results": [{
"sum": 0.0
}]
},
"performanceStats": {
"fileReadCount": 1,
"decompressionCount": 0,
"decompressionCacheEnabledCount": 0
},
"metadata": {
"eventTypes": ["Data_collection"],
"eventType": "Data_collection",
"openEnded": true
}
}, {
"Data":
[{
"name": " Quality_Analyst",
"Domain_Id": "623456",
"app": "slicepay",
"appId": 989766,
"sum": 54
}, {
"name": "Testing",
"Domain_Id": "76554",
"app": "krazybee",
"appId": 1234,
"sum": 54
}],
"totalResult": {
"results": [{
"sum": 2027.0
}]
},
"unknownGroup": {
"results": [{
"sum": 0.0
}]
},
"performanceStats": {
"fileReadCount": 1,
"decompressionCount": 0,
"decompressionCacheEnabledCount": 0
},
"metadata": {
"eventTypes": ["Data_collection"],
"eventType": "Data_collection",
"openEnded": true
}
}]
Try it:
json_data = ... # load your json file
for item in json_data:
new_data = []
for data in item["Data"]:
data_item = dict()
data_item["Domain_name"] = data["name"][0]
data_item["Domain_Id"] = data["name"][1]
data_item["app"] = data["name"][2]
data_item["appId"] = data["name"][3]
data_item["sum"] = data["results"][0]["sum"]
new_data.append(data_item)
item["Data"] = new_data
Output:
[
{'Data': [
{'Domain_name': ' Devlopment',
'Domain_Id': '34876',
'app': 'Tez',
'appId': '4578',
'sum': 54},
{'Domain_name': 'production',
'Domain_Id': '09876',
'app': 'phonepay',
'appId': '2312',
'sum': 50.0}],
'totalResult': {'results': [{'sum': 2027.0}]},
'unknownGroup': {'results': [{'sum': 0.0}]},
'performanceStats':
{'fileReadCount': 1,
'decompressionCount': 0,
'decompressionCacheEnabledCount': 0},
'metadata':
{'eventTypes': ['Data_collection'],
'eventType': 'Data_collection',
'openEnded': True}},
{'Data': [
{'Domain_name': ' Quality_Analyst',
'Domain_Id': '623456',
'app': 'slicepay',
'appId': '989766',
'sum': 54},
...
Hii,
Try this Code:Here json_dic in the sense Your JSON-Data
def change_format(val_lst):
key_lst = ['Domain_name','Domain_Id','app','appId']
res_lst = []
for dic in val_lst:
temp_dic = {key_lst[i]:val for i,val in enumerate(dic['name'])}
temp_dic['sum'] = dic['results'][0]['sum']
res_lst.append(temp_dic)
return res_lst
print(list(map(lambda x:dict([(key,change_format(val_lst)) if key=='Data' else (key,val_lst) for key,val_lst in x.items()]),json_dic)))
Result:
[{'Data': [{'Domain_name': ' Devlopment', 'Domain_Id': '34876', 'app': 'Tez', 'appId': '4578', 'sum': 54}, {'Domain_name': 'production', 'Domain_Id': '09876', 'app': 'phonepay', 'appId': '2312', 'sum': 50.0}], 'totalResult': {'results': [{'sum': 2027.0}]}, 'unknownGroup': {'results': [{'sum': 0.0}]}, 'performanceStats': {'fileReadCount': 1, 'decompressionCount': 0, 'decompressionCacheEnabledCount': 0}, 'metadata': {'eventTypes': ['Data_collection'], 'eventType': 'Data_collection', 'openEnded': 'true'}}, {'Data': [{'Domain_name': ' Quality_Analyst', 'Domain_Id': '623456', 'app': 'slicepay', 'appId': '989766', 'sum': 54}, {'Domain_name': 'Testing', 'Domain_Id': '7654', 'app': 'krazybee', 'appId': '1234', 'sum': 50.0}], 'totalResult': {'results': [{'sum': 2027.0}]}, 'unknownGroup': {'results': [{'sum': 0.0}]}, 'performanceStats': {'fileReadCount': 1, 'decompressionCount': 0, 'decompressionCacheEnabledCount': 0}, 'metadata': {'eventTypes': ['Data_collection'], 'eventType': 'Data_collection', 'openEnded': 'true'}}]
Use my function to change format of new_json data and i just use list comprehension to change new_data.Happy Coding

Pymongo Aggregate with multiple conditions: lookup, unwind, redact, cond, sort and limit

done_status = ['BAD_PU', 'TO_WH', 'RCVDPORT', 'RCVD', 'BAD_DEL', 'MISSFLT', 'OFFLOAD']
shipments = db.db_shipment.aggregate([{
"$lookup":{
"from":"db_shipment_status_history",
"localField":"_id",
"foreignField":"fk_shipment_id",
"as":"shipment_status_history_collection"
}
},
{"$unwind":
"$shipment_status_history_collection"},
{"$redact":{"$cond":{ "$if": { "status_value": {"$in": done_status } } },
"$then": "$$KEEP"
,"$else":"$$PRUNE"
}
},
{"$sort":
{'shipment_status_history_collection.rec_timestamp':-1}},
{"$limit":1},
{"$project":{"pkey":"$pkey","_code":"$_code"}}
])
error:
pymongo.errors.OperationFailure: An object representing an expression must have exactly one field: { $cond: { $if: { status_value: { $in: [ "BAD_PU", "TO_WH", "RCVDPORT", "RCVD", "BAD_DEL", "MISSFLT", "OFFLOAD" ] } } }, $else: "$$PRUNE", $then: "$$KEEP" }
how to fix this error? Im trying to add the latest shipment status history in the shipment record where the status value is in the given status value.
Update the redact stage for your aggregation pipeline. if, then and else are a part of the $cond operator and they're not operators in themselves.
Also, $in operator is passed an array where its first item is checked for presence in the second item. The second item is usually an iterable.
Mongo 3.6
messenger_pipeline_status = (
messenger_active_status['data']['pending']
+ messenger_active_status['data']['processing']
)
assigned_status = ['DEL_ASSIGNED','PU_ASSIGNED']
subpipeline = [
{
'$match': {
'$expr': {
'$and': [
{'$eq': ['$fk_shipment_id', '$$pkey']},
{'$eq': ['$fk_messenger_id', fk_user_id]},
{'$in': ['$status_value', assigned_status]}
]
}
}
},
{
'$sort': {
'rec_timestamp': -1
}
},
{
'$limit': 1
},
{
'$project': {
'fk_shipment_id': 1
}
}
]
pipeline = [
{
'$match': {
'status_value': {'$in': messenger_pipeline_status}
'is_deleted': False,
'is_postponed': False,
'is_active': True,
}
},
{
'$lookup': {
'from': 'db_shipment_status_history',
'let': {'pkey': '$pkey'},
'pipeline': subpipeline,
'as': 'shipment_status_history'
}
},
{
'$match': {
'shipment_status_history': {
'$ne': []
}
}
},
{
'$unwind': '$shipment_status_history'
},
{
'$project': {
'_id': 1,
'pkey': 1,
'_code': 1,
'date_created': 1,
'sender_full_name': '$sender.full_name',
'sender_raw_address': '$sender.raw_address',
'sender_formatted_address': '$sender.formatted_address',
'receiver_full_name': '$receiver.full_name',
'receiver_raw_address': '$receiver.raw_address',
'receiver_formatted_address': '$receiver.formatted_address',
'status_name': 1,
'team_value': 1,
'cs_name': 1,
'fk_messenger_id': '$shipment_status_history.fk_shipment_id'
}
}
]
result = db.db_shipment.aggregate(pipeline)
print(list(result))
[Edit] Mongo 3.2
The following aggregation pipeline produces similar results as the above and is valid query for Mongo 3.2.
messenger_pipeline_status = ['MISSFLT', 'OFFLOAD']
pipeline = [
{
'$match': {
'status_value': { '$in': messenger_pipeline_status}
'is_deleted': False,
'is_postponed': False,
'is_active': True,
}
},
{
"$lookup": {
'from': 'db_shipment_status_history',
'localField': 'pkey',
'foreignField': 'fk_shipment_id',
'as': 'shipment_status_history'
}
},
{
'$match': {
'shipment_status_history': {
'$ne': []
}
}
},
{
'$project': {
'_id': 1,
'pkey': 1,
'_code': 1,
'date_created': 1,
'sender_full_name': '$sender.full_name',
'sender_raw_address': '$sender.raw_address',
'sender_formatted_address': '$sender.formatted_address',
'receiver_full_name': '$receiver.full_name',
'receiver_raw_address': '$receiver.raw_address',
'receiver_formatted_address': '$receiver.formatted_address',
'status_name': 1,
'team_value': 1,
'cs_name': 1,
'shipment_status_history': {
'$filter': {
'input': '$shipment_status_history',
'as': 'shipment',
'cond': {
'$and': [
{'$eq': ['$$shipment.fk_shipment_id', fk_user_id]},
{'$in': ['$$shipment.status_value', assigned_status]},
]
}
}
},
}
},
{
'$unwind': '$shipment_status_history'
},
{
'$sort': {
'shipment_status_history.rec_timestamp': -1,
}
},
{
'$group': {
'_id': '$pkey',
'doc': {
'$first': '$$CURRENT'
}
}
},
{
'$unwind': '$doc'
},
{ # last projection, I promise
'$project': {
'_id': '$doc.id',
'pkey': '$doc.pkey',
'_code': '$doc._code',
'date_created': '$doc.date_created',
'sender_full_name': '$doc.sender_full_name',
'sender_raw_address': '$doc.sender_raw_address',
'sender_formatted_address': '$doc.sender_formatted_address',
'receiver_full_name': '$doc.receiver_full_name',
'receiver_raw_address': '$doc.receiver_raw_address',
'receiver_formatted_address': '$doc.receiver_formatted_address',
'status_name': '$doc.status_name',
'team_value': '$doc.team_value',
'cs_name': '$doc.cs_name',
'fk_messenger_id': '$doc.shipment_status_history.fk_shipment_id'
}
},
]
res = db.db_shipment.aggregate(pipeline)

str to dict in python, but maintain the sequence of json attributes

I've tried ast.literal_eval and json.loads but both of these, doesn't maintain the sequence of json attributes when a string is provided. Please see the following example -
String before providing it to json.loads -
{
"type": "array",
"properties": {
"name": {
"type": "string"
},
"i": {
"type": "integer"
},
"strList": {
"type": "array",
"items": {
"type": "string"
}
},
"strMap": {
"type": "object"
},
"p2": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"i": {
"type": "integer"
},
"p3": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"i": {
"type": "integer"
},
"p4": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"i": {
"type": "integer"
}
}
}
}
}
}
}
},
"p3": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"i": {
"type": "integer"
},
"p4": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"i": {
"type": "integer"
}
}
}
}
}
},
"b": {
"type": "boolean",
"required": true
}
},
"classnames": {
"rootNode": {
"classname": "com.agent.Person"
},
"p2": {
"classname": "com.agent.Person2",
"p3": {
"classname": "com.agent.Person3",
"p4": {
"classname": "com.agent.Person4"
}
}
},
"p3": {
"classname": "com.agent.Person3",
"p4": {
"classname": "com.agent.Person4"
}
}
}
}
String after providing it to json.loads -
{
'classnames': {
'p2': {
'classname': 'com.agent.Person2',
'p3': {
'classname': 'com.agent.Person3',
'p4': {
'classname': 'com.agent.Person4'
}
}
},
'p3': {
'classname': 'com.agent.Person3',
'p4': {
'classname': 'com.agent.Person4'
}
},
'rootNode': {
'classname': 'com.agent.Person'
}
},
'properties': {
'b': {
'required': True,
'type': 'boolean'
},
'i': {
'type': 'integer'
},
'name': {
'type': 'string'
},
'p2': {
'items': {
'properties': {
'i': {
'type': 'integer'
},
'name': {
'type': 'string'
},
'p3': {
'properties': {
'i': {
'type': 'integer'
},
'name': {
'type': 'string'
},
'p4': {
'properties': {
'i': {
'type': 'integer'
},
'name': {
'type': 'string'
}
},
'type': 'object'
}
},
'type': 'object'
}
},
'type': 'object'
},
'type': 'array'
},
'p3': {
'items': {
'properties': {
'i': {
'type': 'integer'
},
'name': {
'type': 'string'
},
'p4': {
'properties': {
'i': {
'type': 'integer'
},
'name': {
'type': 'string'
}
},
'type': 'object'
}
},
'type': 'object'
},
'type': 'array'
},
'strList': {
'items': {
'type': 'string'
},
'type': 'array'
},
'strMap': {
'type': 'object'
}
},
'type': 'array'
}
Can anyone please suggest an alternative or something in python which keeps the sequence of attributes as they are and convert the string into the python dictionary?
As tobias_k has said, python dictionaries are unordered, so you'll lose any order information as soon as you load your data into one.
You can, however, load your JSON string into a OrderedDict:
from collections import OrderedDict
import json
json.loads(your_json_string, object_pairs_hook=OrderedDict)
This method is mentioned in the json module documentation

Categories