HTTP Binding: How to handel different JSON returns

Hello everyone,

I am trying to read data with the HTTP binding

e.g. in case everything is ok I get

{
    “status": ‘starting up’,
    “timestamp": ”2025-04-23T07:42:14.629570+02:00”
}

and in case of an error

{
    “error": ‘Request timed out - trying again with next run’,
    “timestamp": ”2025-04-23T08:42:14.629570+02:00”
}

This means there are sometimes status sometimes error in the JSON data.
How can I handle this, is there a mechanism for this in the binding?
Below is my configuration

UID: http:url:eosConnectRespone
label: EOS Connect Respone HTTP
thingTypeUID: http:url
configuration:
  authMode: BASIC
  ignoreSSLErrors: false
  baseURL: http://192.168.10.40:8081/json/optimize_response.json
  delay: 0
  stateMethod: GET
  refresh: 30
  commandMethod: GET
  contentType: application/json
  timeout: 3000
  bufferSize: 2048
channels:
  - id: last-failure
    channelTypeUID: http:request-date-time
    label: Last Failure
    configuration: {}
  - id: last-success
    channelTypeUID: http:request-date-time
    label: Last Success
    configuration: {}
  - id: error
    channelTypeUID: http:string
    label: Error
    configuration:
      mode: READONLY
      stateTransformation:
        - JSONPATH:$.error
  - id: status
    channelTypeUID: http:string
    label: Status
    configuration:
      mode: READONLY
      stateTransformation:
        - JSONPATH:$.status

Well, it depends :slight_smile:

Either create two channels or use a string channel to get the complete JSON object, then use (e.g.) a JavaScript script to handle both keys and output to one Item.

1 Like

You can chain a regex transformation before the JSONPATH to prevent errors when the message doesn’t match the format that you are looking for in the Channel.

For example, your status channel’s stateTransformation could be

REGEX:(.*(status).*)∩JSONPATH:$.status

Only message that contain the word “status” will get past the REGEX transformation and any message that does not will be dropped without error.

This is what you can do if you want the error and status to be on different Channels.

If you want them to be combined into one Channel the stateTransformation needs to be a Script. But it’s a simple script. In JS:

JS: | (input.includes("error")) ? JSON.parse(input).error : JSON.parse(input).status

Or if you don’t want an inline script you can create a new transformation in Settings → Transformation with the code:

(function(input) {
  var parsed = JSON.parse(input);
  return (parsed.error !== undefined) ? parsed.error : parsed.status;
})(input)

I use the JS script solution, thanks.

JS: | (input.includes("error")) ? JSON.parse(input).error : JSON.parse(input).statu