Update deeply nested property - transform
See original GitHub issueApologies 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:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
You’re nearly there. I think this does what you want:
See https://try.jsonata.org/jAS6Kqp5b
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.