用jq解析不一致的JSON文件

发布于 2025-01-13 06:20:22 字数 988 浏览 4 评论 0原文

我想解析一个 json 文件,该文件是我在集群上配置的 elasticsearch 角色的输出。不幸的是,它的结构在某些地方有所不同,因为一些角色映射是手动添加的,而一些角色映射是通过 API 添加的。

JSON 示例:

{
  "MAPPING_NAME": "FOO_MAPPING",
  "roles": [
    "FOO_ROLE"
  ],
  "rules": {
    "field": {
      "groups": "CN=FO_SEC_GROUP,OU=foo,OU=example,DC=com"
    }
  }
}
{
  "MAPPING_NAME": "BAR_MAPPING",
  "roles": [
    "BAR_ROLE",
    "builtin_role"
  ],
  "rules": {
    "all": [
      {
        "field": {
          "groups": "CN=BAR_SEC_GROUP,OU=bar,OU=example,DC=com"
        }
      }
    ]
  }
}

我想将上面的 json 转换为如下所示,但第一部分具有“规则 -> 字段 -> 组”,第二部分具有“规则 -> 所有 -> 字段 -> 组”。 groups”

{
  "MAPPING_NAME": "FOO_MAPPING",
  "roles": [ 
    "FOO_ROLE"
  ],
  "GROUPS": "CN=FO_SEC_GROUP,OU=foo,OU=example,DC=com"
}
{
  "MAPPING_NAME": "BAR_MAPPING",
  "roles": [
    "BAR_ROLE",
    "builtin_role"
  ],
  "GROUPS": "CN=BAR_SEC_GROUP,OU=bar,OU=example,DC=com"
}

有什么方法可以用jq解析并最终转换为CSV格式吗? 谢谢。

I want to parse a json file that is the output for the elasticsearch roles I configured on the cluster. Unfortunatelly its structure is different in some places, due to the fact that some role mappings were added manually and some roles mapping were added with API.

example JSON:

{
  "MAPPING_NAME": "FOO_MAPPING",
  "roles": [
    "FOO_ROLE"
  ],
  "rules": {
    "field": {
      "groups": "CN=FO_SEC_GROUP,OU=foo,OU=example,DC=com"
    }
  }
}
{
  "MAPPING_NAME": "BAR_MAPPING",
  "roles": [
    "BAR_ROLE",
    "builtin_role"
  ],
  "rules": {
    "all": [
      {
        "field": {
          "groups": "CN=BAR_SEC_GROUP,OU=bar,OU=example,DC=com"
        }
      }
    ]
  }
}

I want to transform the above json to look like below, but the the 1st section has "rules -> fields -> groups" and the 2nd section has "rules -> all -> fields -> groups"

{
  "MAPPING_NAME": "FOO_MAPPING",
  "roles": [ 
    "FOO_ROLE"
  ],
  "GROUPS": "CN=FO_SEC_GROUP,OU=foo,OU=example,DC=com"
}
{
  "MAPPING_NAME": "BAR_MAPPING",
  "roles": [
    "BAR_ROLE",
    "builtin_role"
  ],
  "GROUPS": "CN=BAR_SEC_GROUP,OU=bar,OU=example,DC=com"
}

Is there any way to parse with jq and eventually to transform to CSV format?
Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尽揽少女心 2025-01-20 06:20:22

您可以使用 has 来检查是否存在名为 all 的键:

jq 'del(.rules) + {
  GROUPS: (.rules | if has("all") then .all[] else . end | .field.groups)
}' input.json
{
  "MAPPING_NAME": "FOO_MAPPING",
  "roles": [
    "FOO_ROLE"
  ],
  "GROUPS": "CN=FO_SEC_GROUP,OU=foo,OU=example,DC=com"
}
{
  "MAPPING_NAME": "BAR_MAPPING",
  "roles": [
    "BAR_ROLE",
    "builtin_role"
  ],
  "GROUPS": "CN=BAR_SEC_GROUP,OU=bar,OU=example,DC=com"
}

演示

另一种方法将利用 错误抑制运算符 < code>? 与 替代运算符 //

jq 'del(.rules) + {GROUPS: (.rules | .all[]? // . | .field.groups)}'
{
  "MAPPING_NAME": "FOO_MAPPING",
  "roles": [
    "FOO_ROLE"
  ],
  "GROUPS": "CN=FO_SEC_GROUP,OU=foo,OU=example,DC=com"
}
{
  "MAPPING_NAME": "BAR_MAPPING",
  "roles": [
    "BAR_ROLE",
    "builtin_role"
  ],
  "GROUPS": "CN=BAR_SEC_GROUP,OU=bar,OU=example,DC=com"
}

演示

类似的另一种方法是使用 解构替代运算符 ?//...

You can use has to check for the existence of a key called all:

jq 'del(.rules) + {
  GROUPS: (.rules | if has("all") then .all[] else . end | .field.groups)
}' input.json
{
  "MAPPING_NAME": "FOO_MAPPING",
  "roles": [
    "FOO_ROLE"
  ],
  "GROUPS": "CN=FO_SEC_GROUP,OU=foo,OU=example,DC=com"
}
{
  "MAPPING_NAME": "BAR_MAPPING",
  "roles": [
    "BAR_ROLE",
    "builtin_role"
  ],
  "GROUPS": "CN=BAR_SEC_GROUP,OU=bar,OU=example,DC=com"
}

Demo

Another approach would be to make use of the Error Suppression Operator ? in combination with the Alternative Operator //:

jq 'del(.rules) + {GROUPS: (.rules | .all[]? // . | .field.groups)}'
{
  "MAPPING_NAME": "FOO_MAPPING",
  "roles": [
    "FOO_ROLE"
  ],
  "GROUPS": "CN=FO_SEC_GROUP,OU=foo,OU=example,DC=com"
}
{
  "MAPPING_NAME": "BAR_MAPPING",
  "roles": [
    "BAR_ROLE",
    "builtin_role"
  ],
  "GROUPS": "CN=BAR_SEC_GROUP,OU=bar,OU=example,DC=com"
}

Demo

A similar, yet another approach would be to make use of the Destructuring Alternative Operator ?//...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文