Set new value on nested JSON array/objects - JSONata

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

{
  "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:

I am currently at:

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

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

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

Found it:

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

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