1

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

wp78de
  • 18,207
  • 7
  • 43
  • 71
bearli
  • 73
  • 1
  • 2
  • 6

1 Answers1

2

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.

wp78de
  • 18,207
  • 7
  • 43
  • 71