jslt icon indicating copy to clipboard operation
jslt copied to clipboard

Unable to retrieve the values of the nested loops using JSLT

Open gvishu opened this issue 3 years ago • 1 comments

I want to retrieve the nested array "contacts" values. But when I tried with .contacts, I am not able to get the values of the contact array fields from the input json. Please help me on this. Below is the Input structure and the JSLT transformation being used:

+++++++++++++++++++++++++++input+++++++++++++++ [ { "a" :"12", "b" :"13", "contacts": [ { "Id": "1", "Contact_Id": "1234"

      },
      {
          "Id": "2",
          "Contact_Id": "877656554"
          
      },

{ "Id": "3", "Contact_Id": "333333"

      }
  ]      

}, {

"a" :"12", "b" :"13",
"Watercraft_AdditionalInterests": [ { "Id": "4", "Contact_Id": "77777" }, { "Id": "5", "Contact_Id": "888888"

      },
      {
          "Id": "6",
          "Contact_Id": "9999999"
          
      }
  ]      

} ]

++++++++++++++++JSLT+++++++++++

def range(length, list) if (size($list) < $length) range($length, $list + [size($list)]) else $list

def zip(arr1, arr2)[ for (range(size($arr1), []))[$arr1[.], $arr2[.]]]

{

"requests": flatten(zip([

for (.) {

"body": {
      "data": {
          "attributes": {
            "a" : .a,
            "b" : .b
          }
		} 

    }
   }

],

[ for(..contacts){

"body": { "data": { "attributes": { "contact": { "code": "contact" }, "contacts": { "id": ..Id, "contact id" : ..Contact_Id

                    }
                }
            }
        }
    }]

)) }

gvishu avatar Jul 13 '22 08:07 gvishu

@gvishu

A word of advise, try to format the input as JSON, like so:

[
  {
    "a": "12",
    "b": "13",
    "contacts": [
      {
        "Id": "1",
        "Contact_Id": "1234"
      },
      {
        "Id": "2",
        "Contact_Id": "877656554"
      },
      {
        "Id": "3",
        "Contact_Id": "333333"
      }
    ]
  },
  {
    "a": "12",
    "b": "13",
    "Watercraft_AdditionalInterests": [
      {
        "Id": "4",
        "Contact_Id": "77777"
      },
      {
        "Id": "5",
        "Contact_Id": "888888"
      },
      {
        "Id": "6",
        "Contact_Id": "9999999"
      }
    ]
  }
]

Now the data is clearer.

Next, are you wanting to collect .contacts.Contact_Id as well as .Watercraft_AdditionalInterests.Contact_Id, or only the first one ?

You are not clearly stating what the expected output is from the input above ....

catull avatar Jul 15 '22 19:07 catull