# Set new value on nested JSON array/objects - JSONata

**URL:** <https://discourse.nodered.org/t/set-new-value-on-nested-json-array-objects-jsonata/25172>\
**Category:** General\
**Created:** [20 April 2020 06:05 UTC](https://discourse.nodered.org/t/set-new-value-on-nested-json-array-objects-jsonata/25172 "2020-04-20T06:05:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [20 April 2020 06:05 UTC](https://discourse.nodered.org/t/set-new-value-on-nested-json-array-objects-jsonata/25172/1 "2020-04-20T06:05:08Z")

</div>

I have the following object with nested objects in array's.

```auto
{
  "network": {
    "name": "Internet",
    "properties": {
      "reachable": true,
      "ip": "google.com"
    },
    "children": [
      {
        "name": "router",
        "properties": {
          "reachable": true,
          "ip": "10.0.0.1"
        },
        "children": [
          {
            "name": "switch1",
            "properties": {
              "reachable": true,
              "ip": "10.0.0.2"
            },
            "children": [
              {
                "name": "switch2",
                "properties": {
                  "reachable": true,
                  "ip": "10.0.0.3"
                },
                "children": [
                  {
                    "name": "computer1",
                    "properties": {
                      "reachable": true,
                      "ip": "10.0.0.217"
                    }
                  },
                  {
                    "name": "computer2",
                    "properties": {
                      "reachable": true,
                      "ip": "10.0.0.200"
                    }
                  }
                ]
              },
              {
                "name": "some bridge",
                "properties": {
                  "ip": "10.0.0.202",
                  "reachable": true
                }
              }
            ]
          },
          {
            "name": "AP",
            "properties": {
              "reachable": true,
              "ip": "10.0.0.230"
            },
            "children": [
              {
                "name": "Sonoff Bridge",
                "properties": {
                  "ip": "10.0.0.26",
                  "reachable": true
                }
              },
              {
                "name": "sonoff2",
                "properties": {
                  "ip": "10.0.0.27",
                  "reachable": true
                }
              }
            ]
          }
        ]
      }
    ]
  },
  "ipaddress": "10.0.0.2",
  "state": false
}

```

I want to find/match `ipaddress` to `ip` in the `network` object and set its `reachable` value to the `state` value.

(How) can I do this with jsonata ?

Reading the documentation I suspect I need to use the transform format `... ~> | ... | ... |` but I fail to grasp the concept.

The idea is to build a dynamic network map, eg:

 ![Screenshot 2020-04-20 at 08.13.46](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/8/b845d7d2f7a1fd5e0d2aee8d77e39b2ce0c7b7d1.jpeg)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [20 April 2020 16:17 UTC](https://discourse.nodered.org/t/set-new-value-on-nested-json-array-objects-jsonata/25172/2 "2020-04-20T16:17:20Z")

</div>

I am currently at:

```auto
$ ~>| network[ip=$string(ipaddress)]|{"reachable":$boolean(state)}|  

```

At least it is not rejecting it :') but no success yet.

```auto
**.properties[ip='10.0.0.232'].reachable // works
**.properties[ip=$string($.ipaddress)].reachable // does not work

```

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [20 April 2020 17:28 UTC](https://discourse.nodered.org/t/set-new-value-on-nested-json-array-objects-jsonata/25172/3 "2020-04-20T17:28:57Z")

</div>

Found it:

```auto
**.network ~>|**.properties[$contains(ip,$$.ipaddress)] |{"reachable":$boolean($$.state)} |

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [4 May 2020 17:29 UTC](https://discourse.nodered.org/t/set-new-value-on-nested-json-array-objects-jsonata/25172/4 "2020-05-04T17:29:01Z")

</div>

Closed on 4 May 2020 17:29 UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
