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
2
Answers
You can add
(.groups[].name)
in the string literalSyntax example with reduced field to keep it simple
With all fields (from OP):
OP’s comment, output as a single line:
Use
join()
in the string literal:But removing the string literal and using
join()
or@csv
on an array holding your field makes this a lot easier:You can also store it in a variable:
Demo
The edited output is even simpler: Just iterate over the array:
Demo