jsonlinuxbashjqceph

I'm trying to extract a string from json using jq but got an error "cannot index string with string "num_objects"


My json snippet is as follows



            {
                "bucket": "sample-bucket",
                "tenant": "",
                "num_objects": 189398,
                "num_shards": 0,
                "objects_per_shard": 189398,
                "fill_status": "OVER 100.000000%"
            }

My expected output is sample-bucket and i want to select it using the "num_objects" so i come up with an jq filter below.

cat testjql.txt | jq  -r '.bucket | select(.num_objects == '189398')'

but got an error like this.

jq: error (at <stdin>:8): Cannot index string with string "num_objects"

I tried to play it on different ways but no luck on the expected output.


Solution

  • This filter first selects the object where the "num_objects" property is equal to 189398, and then extracts the value of the "bucket" property from that object,so this should give you the expected output of "sample-bucket"!

    cat testjql.txt | jq -r 'select(.num_objects == 189398).bucket'
    

    output

    sample-bucket