skip to Main Content

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


  1. 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(",")
    
    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search