[英]JSON How to search keys name value and print the objects that contains a string or set of strings with case insensitivity?
Goal:目标:
Un-sanitized test data:未经消毒的测试数据:
{
"PassworD": "dashnd8",
"Name": "Katy"
}
{
"PasSWOrd": "DJNAS98das98",
"Name": "Paulo"
}
{
"Pa$$word": "H(AD*Sn",
"Name": "Crissy"
}
{
"PW": "nA(*DS",
"Name": "Jamel"
}
{
"pW": "0d9asm0i",
"Name": "Denny"
}
sanitized test data:净化测试数据:
{
"Password": "PW",
"Name": "Katy"
}
{
"Password": "pW",
"Name": "Paulo"
}
{
"Password": "pw",
"Name": "Crissy"
}
{
"Password": "passWorD",
"Name": "Jamel"
}
{
"Password": "PAssword",
"Name": "Denny"
}
Note: if the json object has a different Hierarchy please add the necessary steps to access your data注意:如果 json object 有不同的层次结构,请添加必要的步骤来访问您的数据
To sanitize a stream of objects efficiently:要有效地清理对象的 stream:
with_entries( if .key | ascii_downcase | IN("password", "pw", "pa$$word")
then .key="Password"
else . end)
To select efficiently, without sanitizing the results:有效地发送到 select,而无需清理结果:
select( any(keys_unsorted[] | ascii_downcase;
IN("password", "pw", "pa$$word") ) )
To satisfy the search we will use the following:为了满足搜索,我们将使用以下内容:
select()
: pick the key that we will be selecting to search explicitly select()
:选择我们将选择显式搜索的键ascii_downcase
: this modifier is helpful for these type of searches where you do not know if the key has aa certain value. ascii_downcase
:此修饰符对于您不知道键是否具有特定值的这些类型的搜索很有帮助。 This will remove case sensitivity i.e. Tom vs tOm vs toM etc
Tom vs tOm vs toM etc
contains()
: help search for multiple values contains()
: 帮助搜索多个值jq 'select( keys[]| ascii_downcase |contains( "pw","password"))'
BONUS:
However another search might be if you want to search the values based on a specific key in more structured sanitized data where the key name is explicitly the same you would be able to search the values by do this ->但是,如果您想在更结构化的已清理数据中基于特定键搜索值,其中键名称明确相同,则可能是另一种搜索,您可以通过执行此操作来搜索值 ->
jq 'select(.Password| ascii_downcase |contains( "pw","password"))'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.