# (Solved) JSONata - variable to determine correct entry of an Array

**URL:** <https://discourse.nodered.org/t/solved-jsonata-variable-to-determine-correct-entry-of-an-array/6000>\
**Category:** General\
**Created:** [20 December 2018 19:23 UTC](https://discourse.nodered.org/t/solved-jsonata-variable-to-determine-correct-entry-of-an-array/6000 "2018-12-20T19:23:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![IoTPlay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/iotplay/32/40_2.png) [@IoTPlay](https://discourse.nodered.org/u/IoTPlay)\
**Post date:** [20 December 2018 19:23 UTC](https://discourse.nodered.org/t/solved-jsonata-variable-to-determine-correct-entry-of-an-array/6000/1 "2018-12-20T19:23:37Z")

</div>

Hi, I have an array of objects, with an array `ValTypeCat` in ea. object, as follows:

```auto
[{"thing_id":"a1", "val":0, "ValTypeCat":[Off, On]},
 {"thing_id":"a2", "val":1, "ValTypeCat":[Off, On]}]

```

Then, with a Change node, I want to use JSONata to show the ValTypeCat array item that corresponds to the `val` element, ie. `val=1` should have `On`, corresponding to the array entry 1, (vs 0).

To create the payload in the change node, from above incoming msg.payload, and then to show the 1st entry of the `ValTypeCat` array[0], (thus `Off` in above example), the following JSONata expression works:

```auto
payload.(
        { "thing_id" : $.thing_id,
          "val" : $.val,
          "ValTypeCat": $.ValTypeCat[0]
        }
)

```

But, this does not work, it brings back the full array.

```auto
payload.(
        { "thing_id" : $.thing_id,
          "val" : $.val,
          "ValTypeCat": $.ValTypeCat['$.val']
        }
)

```

Any ideas of how to do this?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [20 December 2018 23:00 UTC](https://discourse.nodered.org/t/solved-jsonata-variable-to-determine-correct-entry-of-an-array/6000/2 "2018-12-20T23:00:22Z")

</div>

> [@IoTPlay](#):
>
> Any ideas of how to do this?

```auto
payload.(
	$myval := $.val;
    { "thing_id" : $.thing_id,
    	"val" : $myval,
    	"ValTypeCat": $.ValTypeCat[$myval]
    }
)

```

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [27 December 2018 20:35 UTC](https://discourse.nodered.org/t/solved-jsonata-variable-to-determine-correct-entry-of-an-array/6000/3 "2018-12-27T20:35:14Z")

</div>

This has been the traditional way to "restructure" an array of objects -- iterate over the array and recreate the objects from the original parts...

The more modern (and perhaps simpler?) way would be to use the [object transform syntax](http://docs.jsonata.org/control-operators#transform-). This allows you to just modify an object (add/delete fields, or update existing fields) without affecting other parts of the incoming object(s). Something like this expression should replace the `ValTypeCat` array value with the string at the index # stored in `val`:

```auto
payload ~> | $ | { "ValTypeCat": ($val := val; ValTypeCat[$val]) } |

```

The syntax is reminiscent of regex pattern matching/substitution strings like `m/replace this/with this/`. Essentially, this expression says "for each object in the payload, update the whole object by merging in this new ValTypeCat field value". The new value has to be inside parens because it is a compound statement -- first save the index number `val` in variable `$val`, then use that index to retrieve the correct string from the array of possible strings.

Incidentally, it may be a bit cleaner to use a different field name like `ValType` for the indexed value -- then you can at the same time remove the original field of possible values, using this syntax:

```auto
payload ~> | $ | { "ValType": ($val := val; ValTypeCat[$val]) }, ["ValTypeCat"] |

```

Note the optional 2nd arg, an array of field names to removed from each object. The result now looks like this:

```auto
[
  {
    "thing_id": "a1",
    "val": 0,
    "ValType": "Off"
  },
  {
    "thing_id": "a2",
    "val": 1,
    "ValType": "On"
  }
]

```
