question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Update deeply nested property - transform

See original GitHub issue

Apologies that this is not a real issue related to JSONata, but my misunderstand of its inner works (or maybe it is)

Consider following object:

{
  "ipaddress": "10.0.0.218",
  "network": {
    "children": [
      {
        "children": [
          {
            "children": [
              {
                "children": [
                  {
                    "name": "iMac",
                    "properties": {
                      "ip": "10.0.0.217",
                      "reachable": false
                    }
                  },
                  {
                    "name": "XPenology NAS",
                    "properties": {
                      "ip": "10.0.0.200",
                      "reachable": false
                    }
                  },
                  {
                    "name": "Laser Printer",
                    "properties": {
                      "ip": "10.0.0.100",
                      "reachable": false
                    }
                  }
                ],
                "name": "SWITCH_CF2",
                "properties": {
                  "ip": "10.0.0.3",
                  "reachable": false
                }
              },
              {
                "name": "Home Automation Server",
                "properties": {
                  "ip": "10.0.0.6",
                  "reachable": false
                }
              }
            ],
            "name": "SWITCH_CF1",
            "properties": {
              "ip": "10.0.0.2",
              "reachable": false
            }
          },
          {
            "children": [
              {
                "name": "espressif",
                "properties": {
                  "ip": "10.0.0.218",
                  "reachable": false
                }
              },
              {
                "name": "Sonoff Bridge",
                "properties": {
                  "ip": "10.0.0.26",
                  "reachable": false
                }
              },
              {
                "name": "Sonoff - Printer Power",
                "properties": {
                  "ip": "10.0.0.232",
                  "reachable": false
                }
              },
              {
                "name": "NL0130",
                "properties": {
                  "ip": "10.0.0.234",
                  "reachable": false
                }
              }
            ],
            "name": "UBNT AP",
            "properties": {
              "ip": "10.0.0.230",
              "reachable": false
            }
          }
        ],
        "name": "UBNT ERX",
        "properties": {
          "ip": "10.0.0.1",
          "reachable": false
        }
      }
    ],
    "name": "Internet",
    "properties": {
      "ip": "google.com",
      "reachable": false
    }
  },
  "state": true
}

General idea: find ipaddress in ip and set reachable to the state value, while keeping the network object in tact.

I ended up with a transform expression:

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

This sometimes works, sometimes doesn’t. I use $$.ipaddress and wonder if that correct, something like **.ipaddress (?) but jsonata does not accept it.

Can anyone perhaps steer me in the right direction ?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
andrew-colemancommented, Apr 23, 2020

You’re nearly there. I think this does what you want:

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

See https://try.jsonata.org/jAS6Kqp5b

0reactions
bakman2commented, Apr 23, 2020

I see what went wrong, I had to use the $globalContext object as input instead of the network object that I passed through. Thanks again.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to update a property in a deeply nested array of objects ...
I have a large array of objects that I need to recursively loop through and find the object I need to update by...
Read more >
Recursion to update deeply nested objects
Now in the above array of objects, you have to update the object with the name='xyz', so there are different ways of solving...
Read more >
Deeply Nested Objects and Redux | Pluralsight
For a property update of a deeply nested object to be rendered, the high-level reference needs to be changed. That is, a deep...
Read more >
Modifying Deeply-Nested Structures - nvie.com
Let's update the traverse function with a callback argument that will get called for every node in the structure (every leaf, dict entry,...
Read more >
Updating Objects in State - React Docs
How to correctly update an object in React state; How to update a nested object without mutating it; What immutability is, and how...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found