# Removing elements from JSON, including bringing child elements up a level?

**URL:** <https://discourse.nodered.org/t/removing-elements-from-json-including-bringing-child-elements-up-a-level/59819>\
**Category:** General\
**Created:** [13 March 2022 23:50 UTC](https://discourse.nodered.org/t/removing-elements-from-json-including-bringing-child-elements-up-a-level/59819 "2022-03-13T23:50:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anakaine](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@anakaine](https://discourse.nodered.org/u/anakaine)\
**Post date:** [13 March 2022 23:50 UTC](https://discourse.nodered.org/t/removing-elements-from-json-including-bringing-child-elements-up-a-level/59819/1 "2022-03-13T23:50:33Z")

</div>

I have a JSON string that's being returned by a HTTP Request to an external service. I have a format into which I need this JSON string to be parsed for a library that comes downstream in my flow. I'm not sure where to start on how to manipulate the JSON in node red to remove the elements I don't need, and additionally to move some child elements up to the top.

Any pointers, help, or examples would be very much appreciated.

Input string example:

```auto
{
  "objectIdFieldName" : "OBJECTID", 
  "uniqueIdField" : 
  {
    "name" : "OBJECTID", 
    "isSystemMaintained" : true
  }, 
  "globalIdFieldName" : "", 
  "geometryType" : "esriGeometryPoint", 
  "spatialReference" : {
    "wkid" : 102100, 
    "latestWkid" : 3857
  }, 
  "fields" : [
    {
      "name" : "StationNum", 
      "type" : "esriFieldTypeInteger", 
      "alias" : "StationNum", 
      "sqlType" : "sqlTypeOther", 
      "domain" : null, 
      "defaultValue" : null
    }, 
    {
      "name" : "Height", 
      "type" : "esriFieldTypeDouble", 
      "alias" : "Height", 
      "sqlType" : "sqlTypeOther", 
      "domain" : null, 
      "defaultValue" : null
    }
  ], 
  "features" : [
    {
      "attributes" : {
        "StationNum" : 558105, 
        "Height" : 1.11
      }
    }, 
    {
      "attributes" : {
        "StationNum" : 558018, 
        "Height" : 0.92
      }
    }, 
    {
      "attributes" : {
        "StationNum": 558019,
        "Height": 1.82
       }
   ]

```

What I actually need is just the part from the 'features' section, but I also need to remove the "attributes": {} element. Ie, bring the StationNum and Height up a level. I've also noticed that the StationName, despite being a number, needs to be wrapped with quotes " ".

The top part of the JSON before "features" can be completely dropped.

Example of what I'm after:

```auto
[
    {
        "StationNum": "558105",
        "Height": 1.11
    },
    {
        "tag": "558018",
        "value": 0.92
    },
    {
        "tag": "558019",
        "value": 1.82
    },
]

```

EDIT:  
Looks like a Change node + JSONata might be what I'm after, but am struggling to get it working.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [14 March 2022 02:08 UTC](https://discourse.nodered.org/t/removing-elements-from-json-including-bringing-child-elements-up-a-level/59819/2 "2022-03-14T02:08:27Z")

</div>

The Jsonata would be

```auto
$$.payload.features#$i.attributes.StationNum.{
   ($i=0 ? "StationNum" : "tag"): $string($),
   ($i=0 ? "Height" : "value"): $$.payload.features[$i].attributes.Height
}

```

example flow

```auto
[{"id":"e7e22cfd.b654c","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":" { \"objectIdFieldName\": \"OBJECTID\", \"uniqueIdField\": { \"name\": \"OBJECTID\", \"isSystemMaintained\": true }, \"globalIdFieldName\": \"\", \"geometryType\": \"esriGeometryPoint\", \"spatialReference\": { \"wkid\": 102100, \"latestWkid\": 3857 }, \"fields\": [{ \"name\": \"StationNum\", \"type\": \"esriFieldTypeInteger\", \"alias\": \"StationNum\", \"sqlType\": \"sqlTypeOther\", \"domain\": null, \"defaultValue\": null }, { \"name\": \"Height\", \"type\": \"esriFieldTypeDouble\", \"alias\": \"Height\", \"sqlType\": \"sqlTypeOther\", \"domain\": null, \"defaultValue\": null }], \"features\": [{ \"attributes\": { \"StationNum\": 558105, \"Height\": 1.11 } }, { \"attributes\": { \"StationNum\": 558018, \"Height\": 0.92 } }, { \"attributes\": { \"StationNum\": 558019, \"Height\": 1.82 } }] } ","payloadType":"json","x":250,"y":240,"wires":[["e37d0d84.e3df88"]]},{"id":"e37d0d84.e3df88","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$$.payload.features#$i.attributes.StationNum.{\t ($i=0 ? \"StationNum\" : \"tag\"): $string($),\t ($i=0 ? \"Height\" : \"value\"): $$.payload.features[$i].attributes.Height\t}\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":240,"wires":[["d7bb6032.d2d198"]]},{"id":"d7bb6032.d2d198","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":680,"y":260,"wires":[]}]

```

output

```auto
[
  {
    "StationNum":"558105",
    "Height":1.11
  },
  {
    "tag":"558018",
    "value":0.92
  },
  {
    "tag":"558019",
    "value":1.82
  }
]

```

[edit] for only tag and value

```auto
$$.payload.features#$i.attributes.StationNum.{
   "tag": $string($),
   "value": $$.payload.features[$i].attributes.Height
}

```

---

<div class="post-metadata">

**Author:** ![anakaine](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@anakaine](https://discourse.nodered.org/u/anakaine)\
**Post date:** [14 March 2022 02:19 UTC](https://discourse.nodered.org/t/removing-elements-from-json-including-bringing-child-elements-up-a-level/59819/3 "2022-03-14T02:19:26Z")

</div>

That's awesome! Thank you

I've also made a mistake here - I' accidentally preserved the first element in the example with regards to StationNum/Height. How would I go about modifying your code to ensure that ALL elements have tag/value?

---

<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:** [28 March 2022 02:20 UTC](https://discourse.nodered.org/t/removing-elements-from-json-including-bringing-child-elements-up-a-level/59819/4 "2022-03-28T02:20:05Z")

</div>

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