I have a JSON object with a nested list. Each element from this list can also be a nested list. The depth of each element can go up to n-levels. I need to iterate over all the elements and get the object matching the search criteria. Let's say the item name. If I search for an item name that corresponding object should be returned.
List structure
Nested structure
Below is the sample JSON structure.
{
"Description": "Rooftop heating/ cooling",
"Category": "HVAC",
"PartNumber": "MTU987456",
"Quantity": "1",
"Dimensions": "20(h)X20(b)",
"BomID": "BOM-430",
"Sublocation": "1st Floor",
"GroupID": "BOM-430",
"pxObjClass": "",
"Custom3": "cust3 data",
"Type": "Configured product",
"Custom2": "cust2 data",
"Uom": "each",
"Custom1": "cust1 data",
"ReferenceId": "1552780013",
"Manufacturer": "AirXpert Systems",
"BOMLevel": "0",
"ItemName": "3-6-9 ton Rooftop heating/ cooling",
"Supplier": "Voltas",
"PartRevision": "MTU987456",
"ChildrenBillOfMaterials": [{
"ParentID": "BOM-430",
"Description": "Unit casing",
"Category": "HVAC",
"PartNumber": "U987456",
"Quantity": "1",
"Dimensions": "20(h)X20(b)",
"BomID": "BOM-431",
"Sublocation": "1st Floor",
"GroupID": "BOM-430",
"pxObjClass": "",
"Custom3": "cust3 data",
"Type": "Part",
"Custom2": "cust2 data",
"Uom": "each",
"Custom1": "cust1 data",
"ReferenceId": "15527456",
"Manufacturer": "AirXpert Systems",
"BOMLevel": "1",
"ItemName": "Unit casing",
"Supplier": "Voltas",
"PartRevision": "U987456",
"ChildrenBillOfMaterials": [{
"ParentID": "BOM-431",
"Description": "Metal casing",
"Category": "HVAC",
"PartNumber": "M987456",
"Quantity": "1",
"Dimensions": "20(h)X20(b)",
"BomID": "BOM-432",
"Sublocation": "1st Floor",
"GroupID": "BOM-430",
"pxObjClass": "",
"Custom3": "cust3 data",
"Type": "Part",
"Custom2": "cust2 data",
"Uom": "each",
"Custom1": "cust1 data",
"ReferenceId": "1M5527456",
"Manufacturer": "AirXpert Systems",
"BOMLevel": "2",
"ItemName": "Metal casing",
"Supplier": "Voltas",
"PartRevision": "M987456",
"ChildrenBillOfMaterials": [{
"ParentID": "BOM-432",
"Description": "Fibre casing insulation",
"Category": "HVAC",
"PartNumber": "M98745e",
"Quantity": "1",
"Dimensions": "20(h)X20(b)",
"BomID": "BOM-433",
"Sublocation": "1st Floor",
"GroupID": "BOM-430",
"pxObjClass": "",
"Custom3": "cust3 data",
"Type": "Part",
"Custom2": "cust2 data",
"Uom": "each",
"Custom1": "cust1 data",
"ReferenceId": "1M5C527456",
"Manufacturer": "AirXpert Systems",
"BOMLevel": "3",
"ItemName": "Fibre casing insulation",
"Supplier": "Voltas",
"PartRevision": "M98745e",
"pxUniqueElementID": 1
}],
"pxUniqueElementID": 1
},
{
"ParentID": "BOM-431",
"Description": "Fibre casing",
"Category": "HVAC",
"PartNumber": "M98745e",
"Quantity": "1",
"Dimensions": "20(h)X20(b)",
"BomID": "BOM-445",
"Sublocation": "1st Floor",
"GroupID": "BOM-430",
"pxObjClass": "",
"Custom3": "cust3 data",
"Type": "Part",
"Custom2": "cust2 data",
"Uom": "each",
"Custom1": "cust1 data",
"ReferenceId": "1M5527457",
"Manufacturer": "AirXpert Systems",
"BOMLevel": "2",
"ItemName": "Fibre casing",
"Supplier": "Voltas",
"PartRevision": "M98745e",
"pxUniqueElementID": 2
}
],
"pxUniqueElementID": 1
},
{
"ParentID": "BOM-430",
"Description": "Evaporator coil",
"Category": "HVAC",
"PartNumber": "U774488",
"Quantity": "1",
"Dimensions": "21(h)X21(b)",
"BomID": "BOM-434",
"Sublocation": "2nd Floor",
"GroupID": "BOM-430",
"pxObjClass": "",
"Custom3": "cust6 data",
"Type": "Part",
"Custom2": "cust5 data",
"Uom": "one",
"Custom1": "cust4 data",
"ReferenceId": "205456",
"Manufacturer": "AirXpert Systems",
"BOMLevel": "1",
"ItemName": "Evaporator coil",
"Supplier": "Voltas",
"PartRevision": "U774488",
"pxUniqueElementID": 2
},
{
"ParentID": "BOM-430",
"Description": "Fan assembly",
"Category": "HVAC",
"PartNumber": "U1FN41100",
"Quantity": "1",
"Dimensions": "22(h)X22(b)",
"BomID": "BOM-435",
"Sublocation": "3rd Floor",
"GroupID": "BOM-430",
"pxObjClass": "",
"Custom3": "cust9 data",
"Type": "Part",
"Custom2": "cust8 data",
"Uom": "one",
"Custom1": "cust7 data",
"ReferenceId": "205457",
"Manufacturer": "AirXpert Systems",
"BOMLevel": "1",
"ItemName": "Fan assembly",
"Supplier": "Voltas",
"PartRevision": "U1FN41100",
"ChildrenBillOfMaterials": [{
"ParentID": "BOM-435",
"Description": "Motor",
"Category": "HVAC",
"PartNumber": "U14110MOT0",
"Quantity": "1",
"Dimensions": "22(h)X22(b)",
"BomID": "BOM-436",
"Sublocation": "3rd Floor",
"GroupID": "BOM-430",
"pxObjClass": "",
"Custom3": "cust9 data",
"Type": "Part",
"Custom2": "cust8 data",
"Uom": "one",
"Custom1": "cust7 data",
"ReferenceId": "20MO5457",
"Manufacturer": "AirXpert Systems",
"BOMLevel": "2",
"ItemName": "Motor",
"Supplier": "Voltas",
"PartRevision": "U14110MOT0",
"ChildrenBillOfMaterials": [{
"ParentID": "BOM-436",
"Description": "Rotor",
"Category": "HVAC",
"PartNumber": "U1RR41100",
"Quantity": "1",
"Dimensions": "22(h)X22(b)",
"BomID": "BOM-437",
"Sublocation": "3rd Floor",
"GroupID": "BOM-430",
"pxObjClass": "",
"Custom3": "cust9 data",
"Type": "Part",
"Custom2": "cust8 data",
"Uom": "one",
"Custom1": "cust7 data",
"ReferenceId": "20MORO5457",
"Manufacturer": "AirXpert Systems",
"BOMLevel": "3",
"ItemName": "Rotor",
"Supplier": "Voltas",
"PartRevision": "U1RR41100",
"ChildrenBillOfMaterials": [{
"ParentID": "BOM-437",
"Description": "Steel bearings",
"Category": "HVAC",
"PartNumber": "U1411ST00",
"Quantity": "1",
"Dimensions": "22(h)X22(b)",
"BomID": "BOM-438",
"Sublocation": "3rd Floor",
"GroupID": "BOM-430",
"pxObjClass": "",
"Custom3": "cust9 data",
"Type": "Part",
"Custom2": "cust8 data",
"Uom": "one",
"Custom1": "cust7 data",
"ReferenceId": "20MOBEO5457",
"Manufacturer": "AirXpert Systems",
"BOMLevel": "3",
"ItemName": "Steel bearings",
"Supplier": "Voltas",
"PartRevision": "U1411ST00",
"pxUniqueElementID": 1
}],
"pxUniqueElementID": 1
}],
"pxUniqueElementID": 1
},
{
"ParentID": "BOM-435",
"Description": "Propeller fan",
"Category": "HVAC",
"PartNumber": "U141100",
"Quantity": "1",
"Dimensions": "22(h)X22(b)",
"BomID": "BOM-439",
"Sublocation": "3rd Floor",
"GroupID": "BOM-430",
"pxObjClass": "",
"Custom3": "cust9 data",
"Type": "Part",
"Custom2": "cust8 data",
"Uom": "one",
"Custom1": "cust7 data",
"ReferenceId": "205GO457",
"Manufacturer": "AirXpert Systems",
"BOMLevel": "2",
"ItemName": "Propeller fan",
"Supplier": "Voltas",
"PartRevision": "PR3",
"pxUniqueElementID": 2
}
],
"pxUniqueElementID": 3
},
{
"ParentID": "BOM-430",
"Description": "Compressor",
"Category": "HVAC",
"PartNumber": "U78999",
"Quantity": "1",
"Dimensions": "23(h)X23(b)",
"BomID": "BOM-440",
"Sublocation": "4th Floor",
"GroupID": "BOM-430",
"pxObjClass": "",
"Custom3": "cust12 data",
"Type": "Part",
"Custom2": "cust11 data",
"Uom": "each",
"Custom1": "cust10 data",
"ReferenceId": "205458",
"Manufacturer": "AirXpert Systems",
"BOMLevel": "1",
"ItemName": "Compressor",
"Supplier": "Voltas",
"PartRevision": "U78999",
"pxUniqueElementID": 4
}
]
}
function searchItemByName(sourceObject, name) {
if (sourceObject['ItemName'] === name) {
return sourceObject;
}
if (sourceObject['ChildrenBillOfMaterials']) {
for (let i = 0; i < sourceObject['ChildrenBillOfMaterials'].length; i++) {
let targetObject = searchItemByName(sourceObject['ChildrenBillOfMaterials'][i], name)
if (targetObject !== null) {
return targetObject;
}
}
}
return null;
}