Home > Net >  parsing jq returns null
parsing jq returns null

Time:02-07

I have a json output

{
    "7": [
        {
            "devices": [
                "/dev/sde"
            ],
            "name": "osd-block-dcc9b386-529c-451e-9d84-8ccc4091102b",
            "tags": {
                "ceph.crush_device_class": "None",
                "ceph.db_device": "/dev/nvme0n1p5",
                "ceph.wal_device": "/dev/nvme0n1p6",
            },
            "type": "block",
            "vg_name": "ceph-c4de9e90-853e-4569-b04f-8677ef9a8c7a"
        },
        {
            "path": "/dev/nvme0n1p5",
            "tags": {
                "PARTUUID": "69712eb4-be52-4618-ba46-e317d6d3d76e"
            },
            "type": "db"
        }
    ],
    "41": [
        {
            "devices": [
                "/dev/nvme1n1p13"
            ],
            "name": "osd-block-97bce07f-ae98-4fdb-83a9-9fa2f35cee60",
            "tags": {
                "ceph.crush_device_class": "None",
            },
            "type": "block",
            "vg_name": "ceph-c1d48671-2a33-4615-95e3-cc1b18783f0c"
        }
    ],
    "9": [
        {
            "devices": [
                "/dev/sdf"
            ],
            "name": "osd-block-35323eb8-17c1-460d-8cc5-565f549e6991",
            "tags": {
                "ceph.crush_device_class": "None",
                "ceph.db_device": "/dev/nvme0n1p7",
                "ceph.wal_device": "/dev/nvme0n1p8",
            },
            "type": "block",
            "vg_name": "ceph-9488e8b8-ec18-4860-93d3-6a1ad91c698c"
        },
        {
            "path": "/dev/nvme0n1p7",
            "tags": {
                "PARTUUID": "ef0e9588-2a20-4c2c-8b62-d73945e01322"
            },
            "type": "db"
        }
    ]
}

Required output:

osd.7 /dev/sde /dev/nvme0n1p5 /dev/nvme0n1p6
osd.41 /dev/nvme1n1p13 n/a n/a
osd.9 /dev/sdf /dev/nvme0n1p7 /dev/nvme0n1p7

Problems: When I try parsing using jq .[][].devices, I get null values:

$ cat json | jq .[][].devices
[
  "/dev/sde"
]
null
[
  "/dev/nvme1n1p13"
]
null
[
  "/dev/sdf"
]
null

I can solve it via jq .[][].devices[]?.

However, this trick doesn't help me when I do want to see where there's no value (to print n/a instead):

$ cat json | jq '.[][].tags | ."ceph.db_device"'
"/dev/nvme0n1p5"
null
"/dev/nvme0n1p3"
null
null
"/dev/nvme0n1p7"
null

And finally, I try to create a table:

$ cat json | jq -r '["osd." keys[]], [.[][].devices[]?], [.[][].tags."ceph.db_device" // ""] | @csv' | column -t -s,
"osd.7"           "osd.41"          "osd.9"
"/dev/sde"        "/dev/nvme0n1p13" "/dev/sdf"
"/dev/nvme0n1p5"  "/dev/nvme0n1p7"

So the obvious problem is that the 3rd row doesn't match the correct values.

And the final problem is how do I transpose it from columns to rows, as detailed in the required output?

CodePudding user response:

Would this do what you want?

jq --raw-output '
  to_entries[] | [
    "osd."   .key,
    ( .value[0]
    | .devices[],
      ( .tags
      | ."ceph.db_device" // "n/a",
        ."ceph.wal_device" // "n/a"
      )
    )
  ]
  | @tsv
'
osd.7   /dev/sde    /dev/nvme0n1p5  /dev/nvme0n1p6
osd.41  /dev/nvme1n1p13 n/a n/a
osd.9   /dev/sdf    /dev/nvme0n1p7  /dev/nvme0n1p8

Demo

  •  Tags:  
  • Related