用jq解析不一致的JSON文件
我想解析一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
has
来检查是否存在名为all
的键:演示
另一种方法将利用 错误抑制运算符 < code>? 与 替代运算符
//
:演示
类似的另一种方法是使用 解构替代运算符
?//...
You can use
has
to check for the existence of a key calledall
:Demo
Another approach would be to make use of the Error Suppression Operator
?
in combination with the Alternative Operator//
:Demo
A similar, yet another approach would be to make use of the Destructuring Alternative Operator
?//
...