Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsonPath remove [ ] #484

Open
LucasSantosSilva opened this issue Jul 20, 2018 · 5 comments
Open

JsonPath remove [ ] #484

LucasSantosSilva opened this issue Jul 20, 2018 · 5 comments

Comments

@LucasSantosSilva
Copy link

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
Copy link

hf-kklein commented Jul 24, 2018

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
Copy link
Author

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
Copy link

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

@LucasSantosSilva
Copy link
Author

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
Copy link
Author

I saw now the same problem in other topic

#272

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants