Forum Discussion

arvindnsn's avatar
arvindnsn
Contributor
6 years ago
Solved

How to get Index of Array list along with the data

Hello,

I am working on API’s and while pulling the data from API and storing in the DB. The API is returning data in Arrays and while using the splitter i am able to split the data of arrays and load into the DB.

But my requirement is to also store the Array Index number of each record from the Array along with the data. I tried using the Array function named findIndex(), but in vain.

I would really appreciate if i get a solution for this issue.

Thanks
Aravind N

  • Yes, indexOf() returns the index not the value.

    Using your example data in a json generator, and a mapper with this expression:
    $items.map(value => value.extend({'Data_Index':$items.indexOf(value)}))

    you will see that the original map uses extend to add the index of each element to a new field ‘Data_Index’ in each value in the list.

8 Replies

    • arvindnsn's avatar
      arvindnsn
      Contributor

      I am not looking for a value of a particular Index. If i have 50 items in my Array list, then when i use the splitter to split the Array values, I should have the data look like below

      For example if the Array item returned is like below

      {

      “items”: [

         {
      
            "FIRST_NAME":"Bob",
      
            "LAST_NAME" : "Marley",
      
            "ID": "12"
      
         },
      
         {
      
            "FIRST_NAME":"Eric",
      
            "LAST_NAME" : "Cartman",
      
            "ID": "9"
      
         },
      
         {
      
            "FIRST_NAME":"Ralph",
      
            "LAST_NAME" : "Wreckit",
      
            "ID": "8"
      
         }
      

      ]

      }

      My data should look like

      Firstname Lastname ID Data_Index


      Bob Marley 12 0
      Eric Cartman 9 1
      Ralph Wreckit 8 2

      • cjhoward18's avatar
        cjhoward18
        Employee

        Yes, indexOf() returns the index not the value.

        Using your example data in a json generator, and a mapper with this expression:
        $items.map(value => value.extend({'Data_Index':$items.indexOf(value)}))

        you will see that the original map uses extend to add the index of each element to a new field ‘Data_Index’ in each value in the list.

  • lokey's avatar
    lokey
    New Contributor II

    @cjhoward18 Just wanted to check if the array is split using json splitter can we have sequence snap so documents flowing will generate sequence number for rows?

    Just checking if there are multiple options for this problem to expand more implementation methods.