从 JSON 数据中提取特定值,通过结合两个 jq 命令。

huangapple go评论56阅读模式
英文:

extract specific values from json data by combining 2 jq command

问题

以下是合并后的 jq 命令的翻译部分:

jq -r '.objects[] | "\(.name),\(.uid),\(.type),\(.["ipv4-address"]),\(.["nat-settings"]["ipv4-address"]),\(.["nat-settings"]["ipv6-address"]),\(.["nat-settings"]["install-on"]),\(.["nat-settings"]["method"]),\(.groups[]?.name)"'

希望这可以帮助你获得预期的输出。

英文:

I want to combine below mentioned 2 jq commands to work together in a single jq command.

Command 1:

jq -r '.objects[] | "\(.name),\(.uid),\(.type),\(."ipv4-address"),\(."nat-settings"."ipv4-address"),\(."nat-settings"."ipv6-address"),\(."nat-settings"."install-on"),\(."nat-settings".method)"'

Command 2:

jq -r '.objects[].groups[].name'

I'm able to get output of commands when executed separately but I'm not getting any output after combining both.

Combined command that I used:

jq -r '.objects[] | "\(.name),\(.uid),\(.type),\(.["ipv4-address"]),\(.["nat-settings"]["ipv4-address"]),\(.["nat-settings"]["ipv6-address"]),\(.["nat-settings"]["install-on"]),\(.["nat-settings"]["method"]),\(.groups[]?.name)"'

Please help me correct this command.

Input JSON Code:

{
  "from" : 1,
  "to" : 1,
  "total" : 1,
  "objects" : [ {
    "uid" : "11c758bf-15d7-47ba-af1a-200ffa171587",
    "name" : "BL_2.2.2.2",
    "type" : "host",
    "domain" : {
      "uid" : "41e821a0-3720-11e3-aa6e-0800200c9fde",
      "name" : "SMC User",
      "domain-type" : "domain"
    },
    "ipv4-address" : "2.2.2.2",
    "interfaces" : [ ],
    "nat-settings" : {
      "auto-rule" : true,
      "ipv4-address" : "66.66.66.66",
      "ipv6-address" : "",
      "hide-behind" : "ip-address",
      "install-on" : "All",
      "method" : "hide"
    },
    "groups" : [ {
      "uid" : "eebedbc9-ef19-4c68-91d2-cd6ea3fec11d",
      "name" : "network11111",
      "type" : "group",
      "domain" : {
        "uid" : "41e821a0-3720-11e3-aa6e-0800200c9fde",
        "name" : "SMC User",
        "domain-type" : "domain"
      },
      "members" : [ "11c758bf-15d7-47ba-af1a-200ffa171587" ],
      "groups" : [ ],
      "comments" : "",
      "color" : "black",
      "icon" : "General/group",
      "tags" : [ ],
      "meta-info" : {
        "lock" : "unlocked",
        "validation-state" : "ok",
        "last-modify-time" : {
          "posix" : 1683719146105,
          "iso-8601" : "2023-05-10T17:15+0530"
        },
        "last-modifier" : "admin",
        "creation-time" : {
          "posix" : 1683719146105,
          "iso-8601" : "2023-05-10T17:15+0530"
        },
        "creator" : "admin"
      },
      "read-only" : false,
      "available-actions" : {
        "edit" : "true",
        "delete" : "true",
        "clone" : "true"
      }
    }, {
      "uid" : "f04b042d-0a37-4a10-b470-08ca0e18a129",
      "name" : "workrrorxzdg",
      "type" : "group",
      "domain" : {
        "uid" : "41e821a0-3720-11e3-aa6e-0800200c9fde",
        "name" : "SMC User",
        "domain-type" : "domain"
      },
      "members" : [ "11c758bf-15d7-47ba-af1a-200ffa171587" ],
      "groups" : [ ],
      "comments" : "",
      "color" : "black",
      "icon" : "General/group",
      "tags" : [ ],
      "meta-info" : {
        "lock" : "unlocked",
        "validation-state" : "ok",
        "last-modify-time" : {
          "posix" : 1683719351164,
          "iso-8601" : "2023-05-10T17:19+0530"
        },
        "last-modifier" : "admin",
        "creation-time" : {
          "posix" : 1683719351164,
          "iso-8601" : "2023-05-10T17:19+0530"
        },
        "creator" : "admin"
      },
      "read-only" : false,
      "available-actions" : {
        "edit" : "true",
        "delete" : "true",
        "clone" : "true"
      }
    } ],
    "comments" : "",
    "color" : "black",
    "icon" : "Objects/host",
    "tags" : [ ],
    "meta-info" : {
      "lock" : "unlocked",
      "validation-state" : "ok",
      "last-modify-time" : {
        "posix" : 1683514434876,
        "iso-8601" : "2023-05-08T08:23+0530"
      },
      "last-modifier" : "admin",
      "creation-time" : {
        "posix" : 1683514434876,
        "iso-8601" : "2023-05-08T08:23+0530"
      },
      "creator" : "admin"
    },
    "read-only" : false,
    "available-actions" : {
      "edit" : "true",
      "delete" : "true",
      "clone" : "true"
    }
  } ]
}

Expected Output:

BL_2.2.2.2,11c758bf-15d7-47ba-af1a-200ffa171587,host,2.2.2.2,66.66.66.66,,All,hide,network11111,workrrorxzdg

答案1

得分: 1

你可以在字符串字面量中添加 \(.groups[].name)

简化字段的语法示例:

.objects[] | "\(.name),\(.uid),\(.groups[].name)" 
"BL_2.2.2.2,network11111"
"BL_2.2.2.2,workrrorxzdg"

包括所有字段(来自OP):

.objects[] | "\(.name),\(.uid),\(.type),\(.\"ipv4-address\"),\(.\"nat-settings\".\"ipv4-address\"),\(.\"nat-settings\".\"ipv6-address\"),\(.\"nat-settings\".\"install-on\"),\(.\"nat-settings\".method),\(.groups[].name)" 
"BL_2.2.2.2,11c758bf-15d7-47ba-af1a-200ffa171587,host,2.2.2.2,66.66.66.66,,All,hide,network11111"
"BL_2.2.2.2,11c758bf-15d7-47ba-af1a-200ffa171587,host,2.2.2.2,66.66.66.66,,All,hide,workrrorxzdg"

OP的评论,以单行输出:
使用字符串字面量中的 join()

.objects[] | "\(.name),\(.uid),\(.type),\(.\"ipv4-address\"),\(.\"nat-settings\".\"ipv4-address\"),\(.\"nat-settings\".\"ipv6-address\"),\(.\"nat-settings\".\"install-on\"),\(.\"nat-settings\".method),\([ .groups[].name ] | join(\",\"))"

但删除字符串字面量并在包含字段的数组上使用 join()@csv 会使这个过程更简单:

.objects[] | [ .name, .uid, .type, .groups[].name ] | join(\",\")
英文:

You can add \(.groups[].name) in the string literal

Syntax example with reduced field to keep it simple

.objects[] | "\(.name),\(.uid),\(.groups[].name)" 
"BL_2.2.2.2,network11111"
"BL_2.2.2.2,workrrorxzdg"

With all fields (from OP):

.objects[] | "\(.name),\(.uid),\(.type),\(."ipv4-address"),\(."nat-settings"."ipv4-address"),\(."nat-settings"."ipv6-address"),\(."nat-settings"."install-on"),\(."nat-settings".method),\(.groups[].name)" 
"BL_2.2.2.2,11c758bf-15d7-47ba-af1a-200ffa171587,host,2.2.2.2,66.66.66.66,,All,hide,network11111"
"BL_2.2.2.2,11c758bf-15d7-47ba-af1a-200ffa171587,host,2.2.2.2,66.66.66.66,,All,hide,workrrorxzdg"

OP's comment, output as a single line:
Use join() in the string literal:

.objects[] | "\(.name),\(.uid),\(.type),\(."ipv4-address"),\(."nat-settings"."ipv4-address"),\(."nat-settings"."ipv6-address"),\(."nat-settings"."install-on"),\(."nat-settings".method),\([ .groups[].name ] | join(","))" 

But removing the string literal and using join() or @csv on an array holding your field makes this a lot easier:

.objects[] | [ .name, .uid, .type, .groups[].name ] | join(",")

答案2

得分: 1

你还可以将其存储在一个变量中:

.objects[] | .groups[].name as $groupsname | [ .name, .uid, .type,
  ."ipv4-address", ."nat-settings"."ipv4-address", ."nat-settings"."ipv6-address",
  ."nat-settings"."install-on", ."nat-settings".method, $groupsname
] | join(",")  # 或 @csv
BL_2.2.2.2,11c758bf-15d7-47ba-af1a-200ffa171587,host,2.2.2.2,66.66.66.66,,All,hide,network11111
BL_2.2.2.2,11c758bf-15d7-47ba-af1a-200ffa171587,host,2.2.2.2,66.66.66.66,,All,hide,workrrorxzdg

演示

编辑后的输出更简单:只需对数组进行迭代:

.objects[] | [ .name, .uid, .type,
  ."ipv4-address", ."nat-settings"."ipv4-address", ."nat-settings"."ipv6-address",
  ."nat-settings"."install-on", ."nat-settings".method, .groups[].name
] | join(",")  # 或 @csv
BL_2.2.2.2,11c758bf-15d7-47ba-af1a-200ffa171587,host,2.2.2.2,66.66.66.66,,All,hide,network11111,workrrorxzdg

演示

英文:

You can also store it in a variable:

.objects[] | .groups[].name as $groupsname | [ .name, .uid, .type,
  ."ipv4-address", ."nat-settings"."ipv4-address", ."nat-settings"."ipv6-address",
  ."nat-settings"."install-on", ."nat-settings".method, $groupsname
] | join(",")  # or @csv
BL_2.2.2.2,11c758bf-15d7-47ba-af1a-200ffa171587,host,2.2.2.2,66.66.66.66,,All,hide,network11111
BL_2.2.2.2,11c758bf-15d7-47ba-af1a-200ffa171587,host,2.2.2.2,66.66.66.66,,All,hide,workrrorxzdg

Demo


The edited output is even simpler: Just iterate over the array:

.objects[] | [ .name, .uid, .type,
  ."ipv4-address", ."nat-settings"."ipv4-address", ."nat-settings"."ipv6-address",
  ."nat-settings"."install-on", ."nat-settings".method, .groups[].name
] | join(",")  # or @csv
BL_2.2.2.2,11c758bf-15d7-47ba-af1a-200ffa171587,host,2.2.2.2,66.66.66.66,,All,hide,network11111,workrrorxzdg

Demo

huangapple
  • 本文由 发表于 2023年5月10日 20:42:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218573.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定