Solving this simple(?) JSONPath

96 views Asked by At

I am new to this topic, but I hope you can help me. I can't figure out a correct JSON expression for solving my problem.

Given JSON structure (coming from zigbee2mqtt):

{
  "message" : "announce",
  "meta" : {
    "friendly_name" : "Lamp1"
  },
  "type" : "device_announced"
}

What I am trying:

if $.type == 'device_announced' then return the friendly_name

which in this case is

Lamp1

1

There are 1 answers

0
wp78de On BEST ANSWER

If I understand correctly, you are looking for an expression like this:

$[?(@.type=='device_announced')].meta.friendly_name

So, we are filtering the root collection on types equal to the search string, and then drill down to the friendly_name. You can test this online here.

Note: Some implementations require you to wrap your JSON in an array [ ] to allow this kind of filtering.