Home > Net >  Pipedream - Getting values from nested Json (complex)
Pipedream - Getting values from nested Json (complex)

Time:12-28

an overview of my issue

Am a novice in programming.

Have created a flow in Pipedream, where i converted an xml object to json.

Need to get all values 'name' with format 123456-123-1234 as from a certain level (see picture). The issue is that some levels contain the item straight away, some have multiple items, and therefore another nested array.

The full json array =

    [{"_attributes":{"category":"type","description":"Chassi","factoryDiscount":"none","instance":"0","isCostEdited":"false","isLeaf":"false","isNonStandard":"false","isResolved":"false","isSpecialOption":"false","level":"2.00","listPrice":"24736.00","name":"chassi","netPrice":"11625.92","qty":"1.00","salesPrice":"12615.36"},"items":{"item":{"_attributes":{"category":"type","category2":"type","description":"L06 AC : 0,6t","factoryDiscount":"chassi","instance":"0","isCostEdited":"false","isLeaf":"true","isNonStandard":"false","isResolved":"false","isSpecialOption":"false","level":"3.00","listPrice":"24736.00","name":"117101-1100-005","netPrice":"11625.92","qty":"1.00","salesPrice":"12615.36"}}}},{"_attributes":{"category":"type","description":"Ancillaries","factoryDiscount":"none","instance":"0","isCostEdited":"false","isLeaf":"false","isNonStandard":"false","isResolved":"false","isSpecialOption":"false","level":"2.00","listPrice":"152.00","name":"ancilleries","netPrice":"71.44","qty":"1.00","salesPrice":"77.52"},"items":{"item":[{"_attributes":{"category":"wheels","category2":"wheels","description":"DW polyurethane with tread","factoryDiscount":"ancillary","instance":"0","isCostEdited":"false","isLeaf":"true","isNonStandard":"false","isResolved":"false","isSpecialOption":"false","level":"3.00","listPrice":"152.00","name":"115202-1640-015","netPrice":"71.44","qty":"1.00","salesPrice":"77.52"}},{"_attributes":{"category":"wheels","category2":"wheels","description":"LW Single polyurethane","factoryDiscount":"ancillary","instance":"0","isCostEdited":"false","isLeaf":"true","isNonStandard":"false","isResolved":"false","isSpecialOption":"false","level":"3.00","listPrice":"0.00","name":"115202-1650-005","netPrice":"0.00","qty":"1.00","salesPrice":"0.00"}},{"_attributes":{"category":"mast_options","category2":"mast_options","description":"Central lever LLC","factoryDiscount":"ancillary","instance":"0","isCostEdited":"false","isLeaf":"true","isNonStandard":"false","isResolved":"false","isSpecialOption":"false","level":"3.00","listPrice":"0.00","name":"112000-5100-005","netPrice":"0.00","qty":"1.00","salesPrice":"0.00"}},{"_attributes":{"category":"safety","category2":"safety","description":"Technical Documentation NL","factoryDiscount":"ancillary","instance":"0","isCostEdited":"false","isLeaf":"true","isNonStandard":"false","isResolved":"false","isSpecialOption":"false","level":"3.00","listPrice":"0.00","name":"117101-7850-040","netPrice":"0.00","qty":"1.00","salesPrice":"0.00"}},{"_attributes":{"category":"safety","category2":"safety","description":"RAL 2002 Vermillion","factoryDiscount":"ancillary","instance":"0","isCostEdited":"false","isLeaf":"true","isNonStandard":"false","isResolved":"false","isSpecialOption":"false","level":"3.00","listPrice":"0.00","name":"117101-7900-005","netPrice":"0.00","qty":"1.00","salesPrice":"0.00"}}]}},{"_attributes":{"category":"type","category2":"type","description":"Battery & Charger","factoryDiscount":"none","instance":"0","isCostEdited":"false","isLeaf":"true","isNonStandard":"false","isResolved":"false","isSpecialOption":"false","level":"2.00","listPrice":"0.00","name":"battery_charger","netPrice":"0.00","qty":"1.00","salesPrice":"0.00"}},{"_attributes":{"category":"taxes","description":"Taxes","factoryDiscount":"none","instance":"0","isCostEdited":"false","isLeaf":"false","isNonStandard":"false","isResolved":"false","isSpecialOption":"false","level":"2.00","listPrice":"0.00","name":"taxes","netPrice":"0.00","qty":"1.00","salesPrice":"0.00"},"items":{"item":[{"_attributes":{"category":"taxes","category2":"taxes","description":"Valorlub","factoryDiscount":"none","instance":"0","isCostEdited":"false","isLeaf":"true","isNonStandard":"false","isResolved":"false","isSpecialOption":"false","level":"3.00","listPrice":"0.00","name":"valorlub","netPrice":"0.00","qty":"1.00","salesPrice":"0.00"}},{"_attributes":{"category":"taxes","category2":"taxes","description":"Recytyre cat 1A","factoryDiscount":"none","instance":"0","isCostEdited":"false","isLeaf":"true","isNonStandard":"false","isResolved":"false","isSpecialOption":"false","level":"3.00","listPrice":"0.00","name":"recytyre_cat_1A","netPrice":"0.00","qty":"4.00","salesPrice":"0.00"}}]}}]
 

My Code in a NodJs Step in pipedream:

    export default defineComponent({        
    async run({ steps, $ }) {       
            
    //  let recordb = (steps.xml_to_json.$return_value.resource.attributes.attribute[4].items.item.items.item[1].items.item.length)         
    for (let i = 0; i < steps.xml_to_json.$return_value.resource.attributes.attribute[4].items.item.items.item.length; i  ) {       
    let record = (steps.xml_to_json.$return_value.resource.attributes.attribute[4].items.item.items.item[i].items.item)         
    if (record !== undefined){      
            console.log(record);        
            
    }//if1      
            
    let lth = steps.xml_to_json.$return_value.resource.attributes.attribute[4].items.item.items.item[i].items.item.length;      
            
    if (lth !== undefined){     
            
    //console.log(steps.xml_to_json.$return_value.resource.attributes.attribute[4].items.item.items.item[i].items.item.length);     
    for (let j = 0; j < steps.xml_to_json.$return_value.resource.attributes.attribute[4].items.item.items.item[i].items.item.length; i  ) {                 
    let recordb = (steps.xml_to_json.$return_value.resource.attributes.attribute[4].items.item.items.item[i].items.item[j])         
            
    if (recordb !== undefined){         
            
    console.log(recordb);       
            
    }//if2      
    }//for2     
    }       
    }//for1     
            
            
    //console.log(recordb);     
     },     
    })      

The output:

    { _attributes: { category: 'type', category2: 'type', description: 'L06 AC : 0,6t', factoryDiscount: 'chassi', instance: '0', isCostEdited: 'false', isLeaf: 'true', isNonStandard: 'false', isResolved: 'false', isSpecialOption: 'false', level: '3.00', listPrice: '24736.00', name: '117101-1100-005', netPrice: '11625.92', qty: '1.00', salesPrice: '12615.36' } }
    
    27-12-2022 14:37:39 [ { _attributes: { category: 'wheels', category2: 'wheels', description: 'DW polyurethane with tread', factoryDiscount: 'ancillary', instance: '0', isCostEdited: 'false', isLeaf: 'true', isNonStandard: 'false', isResolved: 'false', isSpecialOption: 'false', level: '3.00', listPrice: '152.00', name: '115202-1640-015', netPrice: '71.44', qty: '1.00', salesPrice: '77.52' } }, { _attributes: { category: 'wheels', category2: 'wheels', description: 'LW Single polyurethane', factoryDiscount: 'ancillary', instance: '0', isCostEdited: 'false', isLeaf: 'true', isNonStandard: 'false', isResolved: 'false', isSpecialOption: 'false', level: '3.00', listPrice: '0.00', name: '115202-1650-005', netPrice: '0.00', qty: '1.00', salesPrice: '0.00' } }, { _attributes: { category: 'mast_options', category2: 'mast_options', description: 'Central lever LLC', factoryDiscount: 'ancillary', instance: '0', isCostEdited: 'false', isLeaf: 'true', isNonStandard: 'false', isResolved: 'false', isSpecialOption: 'false', level: '3.00', listPrice: '0.00', name: '112000-5100-005', netPrice: '0.00', qty: '1.00', salesPrice: '0.00' } }, { _attributes: { category: 'safety', category2: 'safety', description: 'Technical Documentation NL', factoryDiscount: 'ancillary', instance: '0', isCostEdited: 'false', isLeaf: 'true', isNonStandard: 'false', isResolved: 'false', isSpecialOption: 'false', level: '3.00', listPrice: '0.00', name: '117101-7850-040', netPrice: '0.00', qty: '1.00', salesPrice: '0.00' } }, { _attributes: { category: 'safety', category2: 'safety', description: 'RAL 2002 Vermillion', factoryDiscount: 'ancillary', instance: '0', isCostEdited: 'false', isLeaf: 'true', isNonStandard: 'false', isResolved: 'false', isSpecialOption: 'false', level: '3.00', listPrice: '0.00', name: '117101-7900-005', netPrice: '0.00', qty: '1.00', salesPrice: '0.00' } } ]
    
    27-12-2022 14:37:39 { _attributes: { category: 'wheels', category2: 'wheels', description: 'DW polyurethane with tread', factoryDiscount: 'ancillary', instance: '0', isCostEdited: 'false', isLeaf: 'true', isNonStandard: 'false', isResolved: 'false', isSpecialOption: 'false', level: '3.00', listPrice: '152.00', name: '115202-1640-015', netPrice: '71.44', qty: '1.00', salesPrice: '77.52' } }

And get this error: (below)

    TypeError Cannot read property 'item' of undefined
    
    DETAILS at Object.run (file:///tmp/pdg/dist/code/6e17abcbd3b93bd1a5ed7f5b46dabc6403308251df14c794b64e5f9a74ec9378/component.mjs:17:117) at global.executeComponent (/var/task/launch_worker.js:139:53) at MessagePort.messageHandler (/var/task/launch_worker.js:598:28)

CodePudding user response:

Does something like this work?

Basically, check if the item is an Array or not and either loop over it if it is or else grab the name property.

output:

[
  "117101-1100-005",
  "115202-1640-015",
  "115202-1650-005",
  "112000-5100-005",
  "117101-7850-040",
  "117101-7900-005",
  "valorlub",
  "recytyre_cat_1A"
]

See full snippet below:

// console.log(jsonObj);

const itemNames = [];

for (obj of jsonObj) {
  const item = obj?.items?.item;
  // console.log(item);
  if (Array.isArray(item)) {
    for (i of item) {
      const name = i?._attributes?.name;
      if (name) {
        itemNames.push(name);
      }
    }
  }
  else {
    const name = item?._attributes?.name;
    if (name) {
      itemNames.push(name);
    }
  }
}

console.log(itemNames);
<script>
const jsonObj = [
  {
    "_attributes": {
      "category": "type",
      "description": "Chassi",
      "factoryDiscount": "none",
      "instance": "0",
      "isCostEdited": "false",
      "isLeaf": "false",
      "isNonStandard": "false",
      "isResolved": "false",
      "isSpecialOption": "false",
      "level": "2.00",
      "listPrice": "24736.00",
      "name": "chassi",
      "netPrice": "11625.92",
      "qty": "1.00",
      "salesPrice": "12615.36"
    },
    "items": {
      "item": {
        "_attributes": {
          "category": "type",
          "category2": "type",
          "description": "L06 AC : 0,6t",
          "factoryDiscount": "chassi",
          "instance": "0",
          "isCostEdited": "false",
          "isLeaf": "true",
          "isNonStandard": "false",
          "isResolved": "false",
          "isSpecialOption": "false",
          "level": "3.00",
          "listPrice": "24736.00",
          "name": "117101-1100-005",
          "netPrice": "11625.92",
          "qty": "1.00",
          "salesPrice": "12615.36"
        }
      }
    }
  },
  {
    "_attributes": {
      "category": "type",
      "description": "Ancillaries",
      "factoryDiscount": "none",
      "instance": "0",
      "isCostEdited": "false",
      "isLeaf": "false",
      "isNonStandard": "false",
      "isResolved": "false",
      "isSpecialOption": "false",
      "level": "2.00",
      "listPrice": "152.00",
      "name": "ancilleries",
      "netPrice": "71.44",
      "qty": "1.00",
      "salesPrice": "77.52"
    },
    "items": {
      "item": [
        {
          "_attributes": {
            "category": "wheels",
            "category2": "wheels",
            "description": "DW polyurethane with tread",
            "factoryDiscount": "ancillary",
            "instance": "0",
            "isCostEdited": "false",
            "isLeaf": "true",
            "isNonStandard": "false",
            "isResolved": "false",
            "isSpecialOption": "false",
            "level": "3.00",
            "listPrice": "152.00",
            "name": "115202-1640-015",
            "netPrice": "71.44",
            "qty": "1.00",
            "salesPrice": "77.52"
          }
        },
        {
          "_attributes": {
            "category": "wheels",
            "category2": "wheels",
            "description": "LW Single polyurethane",
            "factoryDiscount": "ancillary",
            "instance": "0",
            "isCostEdited": "false",
            "isLeaf": "true",
            "isNonStandard": "false",
            "isResolved": "false",
            "isSpecialOption": "false",
            "level": "3.00",
            "listPrice": "0.00",
            "name": "115202-1650-005",
            "netPrice": "0.00",
            "qty": "1.00",
            "salesPrice": "0.00"
          }
        },
        {
          "_attributes": {
            "category": "mast_options",
            "category2": "mast_options",
            "description": "Central lever LLC",
            "factoryDiscount": "ancillary",
            "instance": "0",
            "isCostEdited": "false",
            "isLeaf": "true",
            "isNonStandard": "false",
            "isResolved": "false",
            "isSpecialOption": "false",
            "level": "3.00",
            "listPrice": "0.00",
            "name": "112000-5100-005",
            "netPrice": "0.00",
            "qty": "1.00",
            "salesPrice": "0.00"
          }
        },
        {
          "_attributes": {
            "category": "safety",
            "category2": "safety",
            "description": "Technical Documentation NL",
            "factoryDiscount": "ancillary",
            "instance": "0",
            "isCostEdited": "false",
            "isLeaf": "true",
            "isNonStandard": "false",
            "isResolved": "false",
            "isSpecialOption": "false",
            "level": "3.00",
            "listPrice": "0.00",
            "name": "117101-7850-040",
            "netPrice": "0.00",
            "qty": "1.00",
            "salesPrice": "0.00"
          }
        },
        {
          "_attributes": {
            "category": "safety",
            "category2": "safety",
            "description": "RAL 2002 Vermillion",
            "factoryDiscount": "ancillary",
            "instance": "0",
            "isCostEdited": "false",
            "isLeaf": "true",
            "isNonStandard": "false",
            "isResolved": "false",
            "isSpecialOption": "false",
            "level": "3.00",
            "listPrice": "0.00",
            "name": "117101-7900-005",
            "netPrice": "0.00",
            "qty": "1.00",
            "salesPrice": "0.00"
          }
        }
      ]
    }
  },
  {
    "_attributes": {
      "category": "type",
      "category2": "type",
      "description": "Battery & Charger",
      "factoryDiscount": "none",
      "instance": "0",
      "isCostEdited": "false",
      "isLeaf": "true",
      "isNonStandard": "false",
      "isResolved": "false",
      "isSpecialOption": "false",
      "level": "2.00",
      "listPrice": "0.00",
      "name": "battery_charger",
      "netPrice": "0.00",
      "qty": "1.00",
      "salesPrice": "0.00"
    }
  },
  {
    "_attributes": {
      "category": "taxes",
      "description": "Taxes",
      "factoryDiscount": "none",
      "instance": "0",
      "isCostEdited": "false",
      "isLeaf": "false",
      "isNonStandard": "false",
      "isResolved": "false",
      "isSpecialOption": "false",
      "level": "2.00",
      "listPrice": "0.00",
      "name": "taxes",
      "netPrice": "0.00",
      "qty": "1.00",
      "salesPrice": "0.00"
    },
    "items": {
      "item": [
        {
          "_attributes": {
            "category": "taxes",
            "category2": "taxes",
            "description": "Valorlub",
            "factoryDiscount": "none",
            "instance": "0",
            "isCostEdited": "false",
            "isLeaf": "true",
            "isNonStandard": "false",
            "isResolved": "false",
            "isSpecialOption": "false",
            "level": "3.00",
            "listPrice": "0.00",
            "name": "valorlub",
            "netPrice": "0.00",
            "qty": "1.00",
            "salesPrice": "0.00"
          }
        },
        {
          "_attributes": {
            "category": "taxes",
            "category2": "taxes",
            "description": "Recytyre cat 1A",
            "factoryDiscount": "none",
            "instance": "0",
            "isCostEdited": "false",
            "isLeaf": "true",
            "isNonStandard": "false",
            "isResolved": "false",
            "isSpecialOption": "false",
            "level": "3.00",
            "listPrice": "0.00",
            "name": "recytyre_cat_1A",
            "netPrice": "0.00",
            "qty": "4.00",
            "salesPrice": "0.00"
          }
        }
      ]
    }
  }
]

</script>

Or if you potentially want to ignore the last 2 names then you can only append those that match a regex pattern

output:

[
  "117101-1100-005",
  "115202-1640-015",
  "115202-1650-005",
  "112000-5100-005",
  "117101-7850-040",
  "117101-7900-005"
]

See full snippet below:

// console.log(jsonObj);

const itemNames = [];
const re = /^\d{6}-\d{4}-\d{3}$/;

for (obj of jsonObj) {
  const item = obj?.items?.item;
  // console.log(item);
  if (Array.isArray(item)) {
    for (i of item) {
      const name = i?._attributes?.name;
      if (re.test(name)) {
        itemNames.push(name);
      }
    }
  }
  else {
    const name = item?._attributes?.name;
    if (re.test(name)) {
      itemNames.push(name);
    }
  }
}

console.log(itemNames);
<script>
const jsonObj = [
  {
    "_attributes": {
      "category": "type",
      "description": "Chassi",
      "factoryDiscount": "none",
      "instance": "0",
      "isCostEdited": "false",
      "isLeaf": "false",
      "isNonStandard": "false",
      "isResolved": "false",
      "isSpecialOption": "false",
      "level": "2.00",
      "listPrice": "24736.00",
      "name": "chassi",
      "netPrice": "11625.92",
      "qty": "1.00",
      "salesPrice": "12615.36"
    },
    "items": {
      "item": {
        "_attributes": {
          "category": "type",
          "category2": "type",
          "description": "L06 AC : 0,6t",
          "factoryDiscount": "chassi",
          "instance": "0",
          "isCostEdited": "false",
          "isLeaf": "true",
          "isNonStandard": "false",
          "isResolved": "false",
          "isSpecialOption": "false",
          "level": "3.00",
          "listPrice": "24736.00",
          "name": "117101-1100-005", // 1
          "netPrice": "11625.92",
          "qty": "1.00",
          "salesPrice": "12615.36"
        }
      }
    }
  },
  {
    "_attributes": {
      "category": "type",
      "description": "Ancillaries",
      "factoryDiscount": "none",
      "instance": "0",
      "isCostEdited": "false",
      "isLeaf": "false",
      "isNonStandard": "false",
      "isResolved": "false",
      "isSpecialOption": "false",
      "level": "2.00",
      "listPrice": "152.00",
      "name": "ancilleries",
      "netPrice": "71.44",
      "qty": "1.00",
      "salesPrice": "77.52"
    },
    "items": {
      "item": [
        {
          "_attributes": {
            "category": "wheels",
            "category2": "wheels",
            "description": "DW polyurethane with tread",
            "factoryDiscount": "ancillary",
            "instance": "0",
            "isCostEdited": "false",
            "isLeaf": "true",
            "isNonStandard": "false",
            "isResolved": "false",
            "isSpecialOption": "false",
            "level": "3.00",
            "listPrice": "152.00",
            "name": "115202-1640-015", // 2
            "netPrice": "71.44",
            "qty": "1.00",
            "salesPrice": "77.52"
          }
        },
        {
          "_attributes": {
            "category": "wheels",
            "category2": "wheels",
            "description": "LW Single polyurethane",
            "factoryDiscount": "ancillary",
            "instance": "0",
            "isCostEdited": "false",
            "isLeaf": "true",
            "isNonStandard": "false",
            "isResolved": "false",
            "isSpecialOption": "false",
            "level": "3.00",
            "listPrice": "0.00",
            "name": "115202-1650-005", // 3
            "netPrice": "0.00",
            "qty": "1.00",
            "salesPrice": "0.00"
          }
        },
        {
          "_attributes": {
            "category": "mast_options",
            "category2": "mast_options",
            "description": "Central lever LLC",
            "factoryDiscount": "ancillary",
            "instance": "0",
            "isCostEdited": "false",
            "isLeaf": "true",
            "isNonStandard": "false",
            "isResolved": "false",
            "isSpecialOption": "false",
            "level": "3.00",
            "listPrice": "0.00",
            "name": "112000-5100-005", // 4
            "netPrice": "0.00",
            "qty": "1.00",
            "salesPrice": "0.00"
          }
        },
        {
          "_attributes": {
            "category": "safety",
            "category2": "safety",
            "description": "Technical Documentation NL",
            "factoryDiscount": "ancillary",
            "instance": "0",
            "isCostEdited": "false",
            "isLeaf": "true",
            "isNonStandard": "false",
            "isResolved": "false",
            "isSpecialOption": "false",
            "level": "3.00",
            "listPrice": "0.00",
            "name": "117101-7850-040", // 5
            "netPrice": "0.00",
            "qty": "1.00",
            "salesPrice": "0.00"
          }
        },
        {
          "_attributes": {
            "category": "safety",
            "category2": "safety",
            "description": "RAL 2002 Vermillion",
            "factoryDiscount": "ancillary",
            "instance": "0",
            "isCostEdited": "false",
            "isLeaf": "true",
            "isNonStandard": "false",
            "isResolved": "false",
            "isSpecialOption": "false",
            "level": "3.00",
            "listPrice": "0.00",
            "name": "117101-7900-005", // 6
            "netPrice": "0.00",
            "qty": "1.00",
            "salesPrice": "0.00"
          }
        }
      ]
    }
  },
  {
    "_attributes": {
      "category": "type",
      "category2": "type",
      "description": "Battery & Charger",
      "factoryDiscount": "none",
      "instance": "0",
      "isCostEdited": "false",
      "isLeaf": "true",
      "isNonStandard": "false",
      "isResolved": "false",
      "isSpecialOption": "false",
      "level": "2.00",
      "listPrice": "0.00",
      "name": "battery_charger",
      "netPrice": "0.00",
      "qty": "1.00",
      "salesPrice": "0.00"
    }
  },
  {
    "_attributes": {
      "category": "taxes",
      "description": "Taxes",
      "factoryDiscount": "none",
      "instance": "0",
      "isCostEdited": "false",
      "isLeaf": "false",
      "isNonStandard": "false",
      "isResolved": "false",
      "isSpecialOption": "false",
      "level": "2.00",
      "listPrice": "0.00",
      "name": "taxes",
      "netPrice": "0.00",
      "qty": "1.00",
      "salesPrice": "0.00"
    },
    "items": {
      "item": [
        {
          "_attributes": {
            "category": "taxes",
            "category2": "taxes",
            "description": "Valorlub",
            "factoryDiscount": "none",
            "instance": "0",
            "isCostEdited": "false",
            "isLeaf": "true",
            "isNonStandard": "false",
            "isResolved": "false",
            "isSpecialOption": "false",
            "level": "3.00",
            "listPrice": "0.00",
            "name": "valorlub",
            "netPrice": "0.00",
            "qty": "1.00",
            "salesPrice": "0.00"
          }
        },
        {
          "_attributes": {
            "category": "taxes",
            "category2": "taxes",
            "description": "Recytyre cat 1A",
            "factoryDiscount": "none",
            "instance": "0",
            "isCostEdited": "false",
            "isLeaf": "true",
            "isNonStandard": "false",
            "isResolved": "false",
            "isSpecialOption": "false",
            "level": "3.00",
            "listPrice": "0.00",
            "name": "recytyre_cat_1A",
            "netPrice": "0.00",
            "qty": "4.00",
            "salesPrice": "0.00"
          }
        }
      ]
    }
  }
]

</script>

  • Related