Home  >  Article  >  Backend Development  >  Golang implementation using JQ with "value in list" condition

Golang implementation using JQ with "value in list" condition

WBOY
WBOYforward
2024-02-08 23:30:11943browse

使用 JQ 的 Golang 实现时具有“列表中的值”条件

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.

Question content

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:

  1. Is there an "in" selector in JQ? What is the correct syntax?
  2. The available "type" list is another field of the same object, along with the store list, how can I read the list directly from this property?

Thanks in advance

Solution

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( ... ).

Demo on jqplay.

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 )

Demo on jqplay.

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!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete