Consider a long list of JSON objects where you want to filter for different criteria. jq can be a big help here.

See the following data:

[
  {
    "lat": 50.559985870799224,
    "lng": 8.478780446017911,
    "price": 400,
    "dist": 67.0682275804,
    "exposeId": "1",
    "ownerSsoId": "100",
    "rooms": 2,
    "area": 74
  },
  {
    "lat": 50.58170215982231,
    "lng": 8.45889590837296,
    "price": 400,
    "dist": 90.9399454663,
    "exposeId": "2",
    "ownerSsoId": "100",
    "rooms": 2,
    "area": 68
  },
  {
    "lat": 50.5622296086318,
    "lng": 8.473003399934473,
    "price": 575,
    "dist": 129.327438312,
    "exposeId": "3",
    "ownerSsoId": "101",
    "rooms": 3,
    "area": 90
  },
  {
    "lat": 50.56314575306962,
    "lng": 8.499306257058922,
    "price": 550,
    "dist": 146.23616163,
    "exposeId": "4",
    "ownerSsoId": "100",
    "rooms": 3,
    "area": 84.31
  },
  {
    "lat": 50.55430884668861,
    "lng": 8.502251769741669,
    "price": 560,
    "dist": 159.412329223,
    "exposeId": "5",
    "ownerSsoId": "100",
    "rooms": 3,
    "area": 93
  }
]

What I want to get is a list of exposeIds for all objects which are not owned by ownerSsoId 101. With jq you can:

  • remove the outer list with ‚.[]‘
  • pass the result to the next expression with ‚|‘
  • select all objects where ownerSsoId is not „101“ with ’select(.ownerSsoId!=“101″)‘
  • pass the result to the next expression with ‚|‘
  • filter the exposeId field with ‚.exposeId‘
curl https://my.api/search | jq '.[]|select(.ownerSsoId!="101")|.exposeId'

What you get as result for the data above:

"1"
"2"
"4"
"5"

A great help to develop the filters and learn to do it the jq stile is https://jqplay.org. Give it a try!

 

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden .