# Change node and jsonata

**URL:** <https://discourse.nodered.org/t/change-node-and-jsonata/16837>\
**Category:** General\
**Created:** [17 October 2019 22:53 UTC](https://discourse.nodered.org/t/change-node-and-jsonata/16837 "2019-10-17T22:53:25Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jtmoderate876](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jtmoderate876/32/6423_2.png) [@jtmoderate876](https://discourse.nodered.org/u/jtmoderate876)\
**Post date:** [17 October 2019 22:53 UTC](https://discourse.nodered.org/t/change-node-and-jsonata/16837/1 "2019-10-17T22:53:26Z")

</div>

Can someone help me understand how to copy/paste/type into the change node in order to have a jsonata query of:  
$msg.payload.(  
value.timeSeries {  
name : {  
"siteName":sourceInfo.siteName,  
"siteCode": sourceInfo.siteCode.value,  
"value" : values.value.value,  
"units" : variable.unit.unitCode,  
"geoLocation": sourceInfo.geoLocation  
}  
}  
)

act upon the incoming payload and the results be in the outgoing msg.payload?  
I'm at wits end.

Here is the query that works on the jsonata test site:  
value.timeSeries {  
name : {  
"siteName":sourceInfo.siteName,  
"siteCode": sourceInfo.siteCode.value,  
"value" : values.value.value,  
"units" : variable.unit.unitCode,  
"geoLocation": sourceInfo.geoLocation  
}  
}

but I don't know how to enter it into the change node:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/7/707875ee6174f68a6307d5d1e5febd09269363d6.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/f/f3971ff19f0cbc5144ccad5eb510aa78ef19bb90.png)

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [18 October 2019 08:27 UTC](https://discourse.nodered.org/t/change-node-and-jsonata/16837/3 "2019-10-18T08:27:04Z")

</div>

Try:  
{  
"siteName":sourceInfo.siteName,  
"siteCode": sourceInfo.siteCode.value,  
"value" : values.value.value,  
"units" : variable.unit.unitCode,  
"geoLocation": sourceInfo.geoLocation  
}

---

<div class="post-metadata">

**Author:** ![jtmoderate876](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jtmoderate876/32/6423_2.png) [@jtmoderate876](https://discourse.nodered.org/u/jtmoderate876)\
**Post date:** [18 October 2019 11:45 UTC](https://discourse.nodered.org/t/change-node-and-jsonata/16837/4 "2019-10-18T11:45:15Z")

</div>

That doesn't work either.  
How do I get the change node to apply my jsonata query to msg.payload?  
The query works here: [http://try.jsonata.org/](http://try.jsonata.org/)  
but I can't get it to work against msg.payload.

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [18 October 2019 12:01 UTC](https://discourse.nodered.org/t/change-node-and-jsonata/16837/5 "2019-10-18T12:01:09Z")

</div>

What does your source json in the payload looks like? While the jsonata playground allows you to directly interact with the json on root level, the values known in the change node aren’t the payload directly, but the attributes on the message object. So if your source `value` is part of the payload you’ve to reference it as `payload.value` in the jsonata for the change node.

So looking at your opening post, rather than $msg.payload, or value directly, use `payload` to reference the payload

Let me actually type this out decently once I have my laptop these edits don’t make it much better

---

<div class="post-metadata">

**Author:** ![jtmoderate876](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jtmoderate876/32/6423_2.png) [@jtmoderate876](https://discourse.nodered.org/u/jtmoderate876)\
**Post date:** [18 October 2019 12:11 UTC](https://discourse.nodered.org/t/change-node-and-jsonata/16837/6 "2019-10-18T12:11:10Z")

</div>

The web service example is:  
[https://waterservices.usgs.gov/nwis/iv/?format=json&stateCd=mo&parameterCd=00060&siteStatus=all&drainAreaMin=650000](https://waterservices.usgs.gov/nwis/iv/?format=json&stateCd=mo&parameterCd=00060&siteStatus=all&drainAreaMin=650000)

It produces the following json:

```auto
{
  "name": "ns1:timeSeriesResponseType",
  "declaredType": "org.cuahsi.waterml.TimeSeriesResponseType",
  "scope": "javax.xml.bind.JAXBElement$GlobalScope",
  "value": {
    "queryInfo": {
      "queryURL": "http://waterservices.usgs.gov/nwis/iv/format=json&stateCd=mo&parameterCd=00060&siteStatus=all&drainAreaMin=650000",
      "criteria": {
        "locationParam": "[]",
        "variableParam": "[00060]",
        "parameter": []
      },
      "note": [
        {
          "value": "[mo]",
          "title": "filter:stateCd"
        },
        {
          "value": "[mode=LATEST, modifiedSince=null]",
          "title": "filter:timeRange"
        },
        {
          "value": "methodIds=[ALL]",
          "title": "filter:methodId"
        },
        {
          "value": "2019-10-18T12:25:34.620Z",
          "title": "requestDT"
        },
        {
          "value": "622dde80-f1a2-11e9-9685-6cae8b6642f6",
          "title": "requestId"
        },
        {
          "value": "Provisional data are subject to revision. Go to http://waterdata.usgs.gov/nwis/help/?provisional for more information.",
          "title": "disclaimer"
        },
        {
          "value": "caas01",
          "title": "server"
        }
      ]
    },
    "timeSeries": [
      {
        "sourceInfo": {
          "siteName": "Mississippi River at St. Louis, MO",
          "siteCode": [
            {
              "value": "07010000",
              "network": "NWIS",
              "agencyCode": "USGS"
            }
          ],
          "timeZoneInfo": {
            "defaultTimeZone": {
              "zoneOffset": "-06:00",
              "zoneAbbreviation": "CST"
            },
            "daylightSavingsTimeZone": {
              "zoneOffset": "-05:00",
              "zoneAbbreviation": "CDT"
            },
            "siteUsesDaylightSavingsTime": true
          },
          "geoLocation": {
            "geogLocation": {
              "srs": "EPSG:4326",
              "latitude": 38.629,
              "longitude": -90.1797778
            },
            "localSiteXY": []
          },
          "note": [],
          "siteType": [],
          "siteProperty": [
            {
              "value": "ST",
              "name": "siteTypeCd"
            },
            {
              "value": "07140101",
              "name": "hucCd"
            },
            {
              "value": "29",
              "name": "stateCd"
            },
            {
              "value": "29510",
              "name": "countyCd"
            }
          ]
        },
        "variable": {
          "variableCode": [
            {
              "value": "00060",
              "network": "NWIS",
              "vocabulary": "NWIS:UnitValues",
              "variableID": 45807197,
              "default": true
            }
          ],
          "variableName": "Streamflow, ft&#179;/s",
          "variableDescription": "Discharge, cubic feet per second",
          "valueType": "Derived Value",
          "unit": {
            "unitCode": "ft3/s"
          },
          "options": {
            "option": [
              {
                "name": "Statistic",
                "optionCode": "00000"
              }
            ]
          },
          "note": [],
          "noDataValue": -999999,
          "variableProperty": [],
          "oid": "45807197"
        },
        "values": [
          {
            "value": [
              {
                "value": "488000",
                "qualifiers": [
                  "P"
                ],
                "dateTime": "2019-10-18T07:00:00.000-05:00"
              }
            ],
            "qualifier": [
              {
                "qualifierCode": "P",
                "qualifierDescription": "Provisional data subject to revision.",
                "qualifierID": 0,
                "network": "NWIS",
                "vocabulary": "uv_rmk_cd"
              }
            ],
            "qualityControlLevel": [],
            "method": [
              {
                "methodDescription": "",
                "methodID": 77445
              }
            ],
            "source": [],
            "offset": [],
            "sample": [],
            "censorCode": []
          }
        ],
        "name": "USGS:07010000:00060:00000"
      }
    ]
  },
  "nil": false,
  "globalScope": true,
  "typeSubstituted": false
}

```

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [18 October 2019 12:11 UTC](https://discourse.nodered.org/t/change-node-and-jsonata/16837/7 "2019-10-18T12:11:54Z")

</div>

I understand, and how does it look on the incoming msg object going into the change node?

I had the same situation where I worked out a difficult object for the first time, with the expression on the json source working in the sandbox editor on the jsonata site. Until I realised that the object incoming was the full message object and not the payload, which makes JSONata even more powerful to me than I previously thought. To solve it, take the root item from the source in the editor on the jsonata site, and in your expression prefix it with `payload.`

---

<div class="post-metadata">

**Author:** ![jtmoderate876](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jtmoderate876/32/6423_2.png) [@jtmoderate876](https://discourse.nodered.org/u/jtmoderate876)\
**Post date:** [18 October 2019 12:30 UTC](https://discourse.nodered.org/t/change-node-and-jsonata/16837/8 "2019-10-18T12:30:51Z")

</div>

Tried adding "payload.":

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/2/2b3ca27c880728b33a8ca27950da97f5aa4a4adc.png)

as this:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/4a1261aac367b3cf01022baf9baeac2de973fa76.png)

but still get nothing in output.

here is node red config:

```auto
[
    {
        "id": "a0081ad3.d51948",
        "type": "inject",
        "z": "c6be1934.85f088",
        "name": "",
        "topic": "",
        "payload": "07010000",
        "payloadType": "str",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": "",
        "x": 80,
        "y": 560,
        "wires": [
            [
                "44fbfb1f.9f1214"
            ]
        ]
    },
    {
        "id": "177b19c4.d23da6",
        "type": "http request",
        "z": "c6be1934.85f088",
        "name": "",
        "method": "GET",
        "ret": "txt",
        "paytoqs": false,
        "url": "",
        "tls": "",
        "persist": false,
        "proxy": "",
        "authType": "",
        "x": 290,
        "y": 600,
        "wires": [
            [
                "db6c6583.d39348",
                "8b21dc79.19e7c"
            ]
        ]
    },
    {
        "id": "8b21dc79.19e7c",
        "type": "debug",
        "z": "c6be1934.85f088",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "x": 770,
        "y": 580,
        "wires": []
    },
    {
        "id": "40a845ba.a4f3cc",
        "type": "change",
        "z": "c6be1934.85f088",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "url",
                "pt": "msg",
                "to": "https://waterservices.usgs.gov/nwis/iv/?format=json&stateCd=mo&parameterCd=00060&siteStatus=all&drainAreaMin=100000",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 310,
        "y": 560,
        "wires": [
            [
                "177b19c4.d23da6"
            ]
        ]
    },
    {
        "id": "44fbfb1f.9f1214",
        "type": "change",
        "z": "c6be1934.85f088",
        "name": "siteid",
        "rules": [
            {
                "t": "set",
                "p": "siteid",
                "pt": "msg",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 190,
        "y": 560,
        "wires": [
            [
                "40a845ba.a4f3cc"
            ]
        ]
    },
    {
        "id": "db6c6583.d39348",
        "type": "change",
        "z": "c6be1934.85f088",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload.value.timeSeries {\t name : {\t \"siteName\":sourceInfo.siteName,\t \"siteCode\": sourceInfo.siteCode.value,\t \"value\" : values.value.value,\t \"units\" : variable.unit.unitCode,\t \"geoLocation\": sourceInfo.geoLocation\t }\t}\t",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 560,
        "y": 620,
        "wires": [
            [
                "41a15677.d1a468"
            ]
        ]
    },
    {
        "id": "41a15677.d1a468",
        "type": "debug",
        "z": "c6be1934.85f088",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "x": 750,
        "y": 620,
        "wires": []
    }
]

```

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [18 October 2019 12:50 UTC](https://discourse.nodered.org/t/change-node-and-jsonata/16837/9 "2019-10-18T12:50:45Z")

</div>

I just ran your flow. First thing I noticed is that the HTTP request node is outputting the whole JSON as a string, rather than an object. Solve that first, either by editing the response type or adding a JSON node after it.  
Once you fix that, you’re getting the output you were after.

---

<div class="post-metadata">

**Author:** ![jtmoderate876](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jtmoderate876/32/6423_2.png) [@jtmoderate876](https://discourse.nodered.org/u/jtmoderate876)\
**Post date:** [18 October 2019 12:53 UTC](https://discourse.nodered.org/t/change-node-and-jsonata/16837/10 "2019-10-18T12:53:09Z")

</div>

THANK YOU!!!! THAT WAS IT. I was looking in the wrong place.
