Change node and jsonata

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:

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

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/
but I can't get it to work against msg.payload.

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

1 Like

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

It produces the following json:

{
  "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³/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
}

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.

Tried adding "payload.":

as this:
image

but still get nothing in output.

here is node red config:

[
    {
        "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": []
    }
]

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.

1 Like

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