1

using jsonpath (https://www.npmjs.com/package/jsonpath) to query an array of objects. I would like to return specific keyed objects from a nested array.

Data:

[
  {
    "payload": {
      "data": {
        "battery": {
          "name": "Battery",
          "value": 45.95860848091411,
          "units": "%",
          "precision": 0
        },
        "humidity": {
          "name": "Humidity",
          "value": 27.84230269716663,
          "units": "%",
          "precision": 2
        },
        "temperature": {
          "name": "Temperature",
          "value": 4.3758105786304675,
          "units": "°",
          "precision": 2
        }
      },
      "timeStamp": 1657356781,
      "deviceId": "172849"
    },
    "timeStamp": "2022-07-09T07:53:00.000Z"
  },
  {
    "payload": {
      "data": {
        "battery": {
          "name": "Battery",
          "value": 15.088388240727735,
          "units": "%",
          "precision": 0
        },
        "humidity": {
          "name": "Humidity",
          "value": 55.695616989217875,
          "units": "%",
          "precision": 2
        },
        "temperature": {
          "name": "Temperature",
          "value": 22.08980374920423,
          "units": "°",
          "precision": 2
        }
      },
      "timeStamp": 1657356840,
      "deviceId": "172849"
    },
    "timeStamp": "2022-07-09T07:55:59.000Z"
  }
]

Desired Result with temperature and humidity only:

[
  {
    "humidity": {
      "name": "Humidity",
      "value": 27.84230269716663,
      "units": "%",
      "precision": 2
    },
    "temperature": {
      "name": "Temperature",
      "value": 4.3758105786304675,
      "units": "°",
      "precision": 2
    }
  },
  {
    "humidity": {
      "name": "Humidity",
      "value": 55.695616989217875,
      "units": "%",
      "precision": 2
    },
    "temperature": {
      "name": "Temperature",
      "value": 22.08980374920423,
      "units": "°",
      "precision": 2
    }
  }
]

I tried this and some other variations to no avail:

jp.query(data, "$..payload.data['humidity']['temperature']");

I can do it with 2 queries, but would like to get it all in one go. thank you for looking.

pb46
  • 13
  • 3
  • `$..payload.data['humidity','temperature']` works in [Jayway JsonPath](https://jsonpath.herokuapp.com/). So it really depends on which library or jsonpath implementation you are using. – Akshay G Jul 19 '22 at 08:46
  • hi yes that does work. I'm using this one: https://www.npmjs.com/package/jsonpath are there other js ones? – pb46 Jul 19 '22 at 08:52
  • `$..payload.data[humidity,temperature]` works in https://jsonpath.com/ that uses https://www.npmjs.com/package/jsonpath-plus but the output is quite different than what you are expecting – Akshay G Jul 19 '22 at 09:01
  • 1
    JMESpath https://jmespath.org/tutorial.html does work with [].payload.{temperature: data.temperature,humidity: data.humidity} – pb46 Jul 19 '22 at 09:22

1 Answers1

0

Ended up using JMESPath

[].payload.{temperature: data.temperature,humidity: data.humidity}
pb46
  • 13
  • 3