Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to retrieve the values of the nested loops using JSLT #262

Open
gvishu opened this issue Jul 13, 2022 · 1 comment
Open

Unable to retrieve the values of the nested loops using JSLT #262

gvishu opened this issue Jul 13, 2022 · 1 comment

Comments

@gvishu
Copy link

gvishu commented Jul 13, 2022

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

                    }
                }
            }
        }
    }]

))
}

@catull
Copy link

catull commented Jul 15, 2022

@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 ....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants