I am trying to create a complex object based on metadata I have. It is an array of attributes which I am iterating and trying to create a dict. For example below is the array
[
"itemUniqueId",
"itemDescription",
"manufacturerInfo[0].manufacturer.value",
"manufacturerInfo[0].manufacturerPartNumber",
"attributes.noun.value",
"attributes.modifier.value",
"attributes.entityAttributes[0].attributeName",
"attributes.entityAttributes[0].attributeValue",
"attributes.entityAttributes[0].attributeUOM",
"attributes.entityAttributes[1].attributeName",
"attributes.entityAttributes[1].attributeValue",
"attributes.entityAttributes[1].attributeUOM",
"attributes.entityAttributes[2].attributeName",
"attributes.entityAttributes[2].attributeValue",
"attributes.entityAttributes[2].attributeUOM",
]
This array should give an output as below
{
"itemUniqueId": "",
"itemDescription": "Item1",
"manufacturerInfo": [
"manufacturer": {
"value": "two"
},
"manufacturerPartNumber": "82"
],
"attributes": {
"noun": {
"value": "1"
},
"modifier": {
"value": "zz"
},
"entityAttributes": [
{
"attributeName": "TYPE",
"attributeValue": "",
"attributeUOM": ""
},
{
"attributeName": "SIZE",
"attributeValue": "3",
"attributeUOM": "IN"
}
]
}
}
I have written this logic but unable to get the desired output. It should work on both object and array given the metadata.
source_json = [
"itemUniqueId",
"itemDescription",
"manufacturerInfo[0].manufacturer.value",
"manufacturerInfo[0].manufacturerPartNumber",
"attributes.noun.value",
"attributes.modifier.value",
"attributes.entityAttributes[0].attributeName",
"attributes.entityAttributes[0].attributeValue",
"attributes.entityAttributes[0].attributeUOM",
"attributes.entityAttributes[1].attributeName",
"attributes.entityAttributes[1].attributeValue",
"attributes.entityAttributes[1].attributeUOM",
"attributes.entityAttributes[2].attributeName",
"attributes.entityAttributes[2].attributeValue",
"attributes.entityAttributes[2].attributeUOM",
]
for row in source_json:
propertyNames = row.split('.')
temp = ''
parent = {}
parentArr = []
parentObj = {}
#if len(propertyNames) > 1:
arrLength = len(propertyNames)
for i, (current) in enumerate(zip(propertyNames)):
if i ==0:
if '[' in current:
parent[current]=parentArr
else:
parent[current] = parentObj;
temp = current
if i>0 and i<arrLength-1:
if '[' in current:
parent[current]=parentArr
else:
parent[current] = parentObj;
temp = current
if i == arrLength-1:
if '[' in current:
parent[current]=parentArr
else:
parent[current] = parentObj;
temp = current
#temp[prev][current] =""
#finalMapping[target] = target
print(parent)
Any help is much appreciated, Thanks in advance
from Create complex object in python based on property names
No comments:
Post a Comment