Home > OS >  Get Father and Children in an API with APPS SCRIPT (GSHEETS)
Get Father and Children in an API with APPS SCRIPT (GSHEETS)

Time:09-06

i'm using google apps script, and I have a JSON array consisting of nested parent and child objects.

  "logisticalHierarchies": [
            {
                "product_key_id": 48232671,
                "gtin": "05449000696878",
                "lastRequest": null,
                "productIdentifier": null,
                "children": [
                    {
                        "product_key_id": 48232673,
                        "gtin": "05449000283863",
                        "quantity": 130,
                        "productIdentifier": null,
                        "children": [
                            {
                                "product_key_id": 48232457,
                                "gtin": "05449000283856",
                                "quantity": 4,
                                "productIdentifier": null,
                                "children": [
                                    {
                                        "product_key_id": 48232675,
                                        "gtin": "05449000214843",
                                        "quantity": 6,
                                        "productIdentifier": null,
                                        "children": [],
                                        "contentOwner_id": 10525,
                                        "isMainHierarchyUnit": false,

I would like by entering the GTIN object as parameters, to succeed in recovering the GTIN object of the father of the product that I have just entered.

For example if I enter the GTIN: 05449000283856 I get the GTIN FATHER: 05449000283863

For the moment I am able to retrieve only the first GTIN of the list (the first father) using this script:

  var url='https://apis.alkemics.com/public/v1/products?' params;
  //Logger.log(url);
  var content =UrlFetchApp.fetch(url, options);
  //Logger.log(content);
  //Logger.log(content.getResponseCode())
  if (content. getResponseCode() ==200) {
    var return =JSON.parse(content.getContentText());
    next_page=back.next_page;
    var data=return.data;

    for(i=0; i<data.length;i  ) {
      var product=data[i]; // A product in JSON format
      
      
      var childrens = data.map(({logisticalHierarchies}) => logisticalHierarchies.map(o => [o.children?.gtin || ""]));
      Logger.log(childrens)

      var line=[
        product.gtin,
        product.logisticalHierarchies[0] != null? product.logisticalHierarchies[0].children[0].gtin: ' ',
        

      ];

CodePudding user response:

So a recursion would help, passing the father as you go into deeper level. This one written for readability not for efficiency as you should break from the loop early if you find the searched item.

var logisticalHierarchies = [{
  "product_key_id": 48232671,
  "gtin": "05449000696878",
  "lastRequest": null,
  "productIdentifier": null,
  "children": [{
    "product_key_id": 48232673,
    "gtin": "05449000283863",
    "quantity": 130,
    "productIdentifier": null,
    "children": [{
      "product_key_id": 48232457,
      "gtin": "05449000283856",
      "quantity": 4,
      "productIdentifier": null,
      "children": [{
        "product_key_id": 48232675,
        "gtin": "05449000214843",
        "quantity": 6,
        "productIdentifier": null,
        "children": [],
        "contentOwner_id": 10525,
        "isMainHierarchyUnit": false,
      }, {
        "product_key_id": 12323,
        "gtin": "05449000214847",
        "quantity": 6,
        "productIdentifier": null,
        "children": [],
        "contentOwner_id": 10525,
        "isMainHierarchyUnit": false,
      }]
    }]
  }]
}]

function find_gtin_father(arr, gtin, parent) {
  parent = parent || null;
  var found = null;
  arr.forEach(function(item) {
    if (item.gtin === gtin) {
      found = parent;
    } else {
      if (item.children) {
        found = found || find_gtin_father(item.children, gtin, item);
      }
    }
  })
  return found;
}

var item = find_gtin_father(logisticalHierarchies, "05449000214847")
console.log(item)
.as-console-wrapper {max-height: 100% !important}

  • Related