json-path / JsonPath

Java JsonPath implementation
Apache License 2.0
8.78k stars 1.63k forks source link

JsonPath remove [ ] #484

Open LucasSantosSilva opened 6 years ago

LucasSantosSilva commented 6 years ago

how to remove [] in my result in JsonPath, I saw in other topics using id, but I not have ID. This is code I use:

[{"timestamp":"20180719T11:45:24Z",
"type":"S1",
"mac":"AC233FA03DB6",
"bleName":"",
"rssi":-53,
"battery":100,
"temperature":20.18,
"humidity":63.02}]

and this is the filter

$..[?(@.mac=='AC233FA03DB6' )].temperature

I use this site for test my examples: (http://jsonpath.herokuapp.com/?path=$..book[2:])

hf-kklein commented 5 years ago

Hi, the result of your querypath $..[?(@.mac=='AC233FA03DB6' )].temperature is a list (surrounded by [and ]) because there could be more than one entry in you JSON having an attribute "mac" with value AC233FA02DB6. Try to use your path on this JSON:

[
  {
    "timestamp": "20180719T11:45:24Z",
    "type": "S1",
    "mac": "AC233FA03DB6",
    "bleName": "",
    "rssi": -53,
    "battery": 100,
    "temperature": 20.18,
    "humidity": 63.02
  },
  {
    "timestamp": "someothertime",
    "type": "S2",
    "mac": "AC233FA03DB6",
    "bleName": "",
    "rssi": -54,
    "battery": 50,
    "temperature": 12,
    "humidity": 80.02
  }
]

Then the result is

[
   20.18,
   12.0
]

If you would like to only get one single value, without the list, you have to use a distinct path. For example the corresponding normalised path $[0]['temperature'] which returns 20.18 for both your and my JSON example. I don't know if you can change the data structure but if mac is a unique attribute you could use it as a key in a dictionary/object like this:

{
  "AC233FA03DB6": {
    "timestamp": "20180719T11:45:24Z",
    "type": "S1",
    "bleName": "",
    "rssi": -53,
    "battery": 100,
    "temperature": 20.18,
    "humidity": 63.02
  },
  "ANOTHERMAC": {
    "timestamp": "someothertime",
    "type": "S2",
    "bleName": "",
    "rssi": -54,
    "battery": 50,
    "temperature": 12,
    "humidity": 80.02
  }
}

Then the distinct path$['AC233FA03DB6'].temperature returns just 20.18, without the list.

LucasSantosSilva commented 5 years ago

Almost solved my problem. I will explain to you, I have several sensors and two temperature sensors, when the Json package arrives it sometimes sends in random order, that way I would have to use the mac as a filter because it contains more sensors and the position always changes.

This is my Json package

[{"timestamp":"2018-07-24T14:04:48Z","type":"iBeacon","mac":"AC233F252A0C","bleName":"","ibeaconUuid":"E2C56DB5DFFB48D2B060D0F5A71096E0","ibeaconMajor":0,"ibeaconMinor":0,"rssi":-46,"ibeaconTxPower":-59,"battery":0},

{"timestamp":"2018-07-24T14:04:49Z","type":"iBeacon","mac":"AC233F2539C2","bleName":"","ibeaconUuid":"E2C56DB5DFFB48D2B060D0F5A71096E0","ibeaconMajor":0,"ibeaconMinor":0,"rssi":-46,"ibeaconTxPower":-59,"battery":0},

{"timestamp":"2018-07-24T14:04:49Z","type":"iBeacon","mac":"AC233F2539D3","bleName":"","ibeaconUuid":"E2C56DB5DFFB48D2B060D0F5A71096E0","ibeaconMajor":0,"ibeaconMinor":0,"rssi":-52,"ibeaconTxPower":-59,"battery":0},

{"timestamp":"2018-07-24T14:04:49Z","type":"S1","mac":"AC233FA03DB6","bleName":"","rssi":-48,"battery":100,"temperature":21.73,"humidity":64.42},

{"timestamp":"2018-07-24T14:04:49Z","type":"S1","mac":"AC233FA03E16","bleName":"","rssi":-53,"battery":100,"temperature":20.81,"humidity":62.39}

]

if I use $[0]['temperature'] I need change everytime the position, so, I need filter with your mac and get the value of temperature.

hf-kklein commented 5 years ago

Why don't you just use the first value of the returned array?

LucasSantosSilva commented 5 years ago

What I want you to understand is that array information can vary, temperature and humidity may come in the [0], [2] or [5] for example, which implies that I'm using an application called mqtt dashboard, when I create a dashboard I put as for example $[0]['temperature'] but if I connect another sensor it may be that the array where it is the temperature and humidity go to the array [1], I just do not want to have to keep changing in the application in which array the value will arrive. So I used a filter for the mac, because it is a fixed value that the sensors send.

LucasSantosSilva commented 5 years ago

I saw now the same problem in other topic

https://github.com/json-path/JsonPath/issues/272