JSON 如何搜索键名称值并打印包含不区分大小写的字符串或字符串集的对象?

JSON How to search keys name value and print the objects that contains a string or set of strings with case insensitivity?

目标:

未过滤的测试数据:

{
    "PassworD": "dashnd8",
    "Name": "Katy"
}
{
    "PasSWOrd": "DJNAS98das98",
    "Name": "Paulo"
}
{
    "Pa$$word": "H(AD*Sn",
    "Name": "Crissy"
    
}
{
    "PW": "nA(*DS",
    "Name": "Jamel"
    
}
{
    "pW": "0d9asm0i",
    "Name": "Denny"
}

消毒测试数据:

{
    "Password": "PW",
    "Name": "Katy"
}
{
    "Password": "pW",
    "Name": "Paulo"
}
{
    "Password": "pw",
    "Name": "Crissy"
    
}
{
    "Password": "passWorD",
    "Name": "Jamel"
    
}
{
    "Password": "PAssword",
    "Name": "Denny"
}

注意:如果 json 对象具有不同的层次结构,请添加必要的步骤来访问您的数据

为了满足搜索,我们将使用以下内容:

  • select():选择我们将选择显式搜索的键
  • ascii_downcase:此修饰符有助于您不知道键是否具有特定值的此类搜索。这将消除区分大小写 Tom vs tOm vs toM etc
  • contains(): 帮助搜索多个值

jq 'select( keys[]| ascii_downcase |contains( "pw","password"))'

BONUS:

然而,如果您想在更结构化的清理数据中基于特定键搜索值,其中键名明确相同,您可以通过 do 搜索值这个 ->

jq 'select( .Password| ascii_downcase |contains( "pw","password"))'

有效清理对象流:

with_entries( if .key | ascii_downcase | IN("password", "pw", "pa$$word")
              then .key="Password" 
              else . end) 

为了 select 高效,而不对结果进行清理:

select( any(keys_unsorted[] | ascii_downcase;
            IN("password", "pw", "pa$$word") ) )