Home > Article > Backend Development > Golang implementation using JQ with 'value in list' condition
What php editor Xiaoxin is going to introduce today is the "value in list" condition when using JQ's Golang implementation. In Golang, JQ is a powerful query language for extracting and transforming information from JSON data. The "value in list" condition is a condition commonly used in JQ queries. It can be used to filter out elements in the list that contain specified values. By using the Golang implementation of JQ, we can easily implement this function, thereby simplifying our processing of JSON data and improving development efficiency. Next, we'll detail how to implement the "value in list" condition using the Golang implementation of JQ.
I use this library in my Go application: https://github.com/itchyny/gojq It works fine, but I can't find how to do something in the JQ documentation. I have a jq query whose input is the list of available stores from the configuration file like this:
( [.stores[] | select( .is_open == true )] | length > 1 ) and ( [.stores[] | select( .name == "Some store" )] | length == 1 )
Basically it says, from the list of available stores in my area, at least one is open, and, from the list of available stores in my area, at least one is called "some store". (Note that it's two separate conditions, I don't care if "Some store" is closed).
Now I need to change this condition so that each store will have a "type_identifier" which is a string instead of using the second condition to see the name. Basically what I want is something like this:
( [.stores[] | select( .type_identifier in ["type1","type2", "type2"...] )] | length == 1 )
The list of available types has about 600 different types. So my question is:
Thanks in advance
If the list is that long, it's better to create a lookup table.
( [ "type1", "type2", "type3" ] | map( { (.): true } ) | add ) as $lkup | .stores | ( map( select( .is_open ) ) | length > 1 ) and ( map( select( $lkup[ .type_identifier ] ) ) | length == 1 )
I decomposed the common .stores
and replaced it with [ .[] | ... ]
and map( ... )
.
You can also use INDEX
.
( [ "type1", "type2", "type3" ] | INDEX( .[]; . ) ) as $lkup | .stores | ( map( select( .is_open ) ) | length > 1 ) and ( map( select( $lkup[ .type_identifier ] ) ) | length == 1 )
The above is the detailed content of Golang implementation using JQ with 'value in list' condition. For more information, please follow other related articles on the PHP Chinese website!