简体   繁体   English

bash - jq 迭代 json,键和值中有空格

[英]bash - jq iterating over json with spaces in key and value

Trying to iterate the top level keys and corresponding json object value from a file with following structure.尝试从具有以下结构的文件中迭代顶级键和相应的 json object 值。

{
   "parkey1":{
      "parkey1subkey1":"sub val1",
      "parkey1subkey2":"sub val2"
   },
   "parkey2":{
      "parkey2subkey1":{
         "subsubkey1":"val with space",
         "subsubkey2":{
            "subsubval3":"val space"
         }
      }
   }
}

The above structure is just a sample.上述结构只是一个示例。 The keys and values can be anything.键和值可以是任何东西。 The expected output is to get the keys and corresponding json object values.预期的 output 是获取密钥和对应的 json object 值。 With to_entries the spaces between the string values are breaking the logic.使用 to_entries 字符串值之间的空格打破了逻辑。

Desired output:所需的 output:

key: "parkey1"
value: {
      "parkey1subkey1":"sub val1",
      "parkey1subkey2":"sub val2"
   }

key: parkey2
value: {
      "parkey2subkey1":{
         "subsubkey1":"val with space",
         "subsubkey2":{
            "subsubval3":"val space"
         }
      }
   }
jq 'to_entries[]' | sed -e 's/^[{}]//' -e 's/^  //'

If the quotes around the top-level key and value tokens are an issue, you can easily extend the sed directives based on the uniform indentation.如果顶级keyvalue标记周围的引号有问题,您可以基于统一缩进轻松扩展 sed 指令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM