How to add an index number to each object of an array

You sometimes face a requirement where you should add an index number to each object of an array. For example, some systems require you to add a line number to each order lines within an order entity. You can use this guide to generate the index numbers.

Let’s assume that we have the below data.

{
  "data": [
    {
      "name": "foo"
    },
    {
      "name": "bar"
    },
    {
      "name": "baz"
    }
  ]
}

Your objective is to add an index number to each object of the data array, such as below.

{
  "data": [
    {
      "index": 1,
      "name": "foo"
    },
    {
      "index": 2,
      "name": "bar"
    },
    {
      "index": 3,
      "name": "baz"
    }
  ]
}

In order to achieve that, firstly, use Move data between accessors to generate an index for each object of an array. By default, the index starts with 0.

The output of the “Move data between accessors” is below.

{
  "data": {
    "3": {
      "index": "0",
      "value": {
        "name": "foo"
      }
    },
    "4": {
      "index": "1",
      "value": {
        "name": "bar"
      }
    },
    "5": {
      "index": "2",
      "value": {
        "name": "baz"
      }
    }
  }
}

Now, we have an index number for each object, and it starts with 0. However, the index of the array starts with number 3 instead of 0. The array is now an associative array, not an indexed array due to having irregular index. This issue can be fixed by resetting the array index. It can be done using a Value Mapper and the “List: Get Values” mapper.

It will reset the index of the data array, such as below.

{
  "data": [
    {
      "index": "0",
      "value": {
        "name": "foo"
      }
    },
    {
      "index": "1",
      "value": {
        "name": "bar"
      }
    },
    {
      "index": "2",
      "value": {
        "name": "baz"
      }
    }
  ]
}

If you want the index property starts with 1 instead of 0, you can increase the numbers by 1 using Value Mapper and Prepared Operator. We will use Addition operator in this case.

Now you have the index number starts with 1.

{
  "data": [
    {
      "index": 1,
      "value": {
        "name": "foo"
      }
    },
    {
      "index": 2,
      "value": {
        "name": "bar"
      }
    },
    {
      "index": 3,
      "value": {
        "name": "baz"
      }
    }
  ]
}

However, all the original properties of each object are now under the values property. You should move them to the root of each object. Use Move using a pattern to move them at once.

You have successfully moved the original properties back to its original places.

{
  "data": [
    {
      "index": 1,
      "value": [],
      "name": "foo"
    },
    {
      "index": 2,
      "value": [],
      "name": "bar"
    },
    {
      "index": 3,
      "value": [],
      "name": "baz"
    }
  ]
}

The last thing to do is to remove the value property that previously holds the original properties of each object. In order to do that, you can use Key Filter, such as below.

Finally, you will have the data you want to achieve.

{
  "data": [
    {
      "index": 1,
      "name": "foo"
    },
    {
      "index": 2,
      "name": "bar"
    },
    {
      "index": 3,
      "name": "baz"
    }
  ]
}
2 Likes