简体   繁体   English

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

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

Goal:目标:

  • Search the keys actual name value for a string or set of strings搜索字符串或字符串集的键实际名称值
  • search using case insensitive values使用不区分大小写的值进行搜索
  • return the object(s) containing the values返回包含值的对象

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.

相关问题 如何检查 Json 对象和 Json 数组中的属性不区分大小写 - how to check case insensitivity of properties in Json object and Json Arrays 如果名称包含点,如何获取 JSON 对象值? - How to get JSON objects value if its name contains dots? 空手道:如果对象名称在数组中包含句点,如何设置 Json 值 - karate : How to set the Json value if object name contains period in an array 如果包含 json 文档作为字符串,如何在 MySQL(5.6) 列中设置值 - How to set value in MySQL(5.6) column if that contains json document as string 如何在JSON对象内的对象数组中搜索字符串并返回包含该对象的对象? - How to search through an array of objects inside a JSON object for a string and return the object that contains it? 如何检查 json 内部的值并获取包含具有该值的键的 object? - How to check if value inside of json and get the object that contains the keys with that value? 在JSON数组中搜索字符串并检索包含它作为值的对象 - Search JSON Array for a String and Retrieve Object that Contains it as a Value 如何使用Javascript React打印包含html的对象值 - How to print an objects value that contains html with Javascript React 如何使用 Eloquent 在包含对象数组的 json 字段中进行搜索 - How to search in a json field that contains an array of objects with Eloquent 将包含字符串键的 JSON 文件解码为 [(Date,Double)],每个字符串键包含一个键值对 - Decode a JSON file that contains string keys each of which contains one key value pair into [(Date,Double)]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM