I have a below curl which passes the value 2000 in the jq command which returns the right value...

curl -X GET \
  -H 'vmware-api-session-id: <auth-id>' \
  -k -s
  'https://<url>/api/vcenter/vm/vm-22' | python -m json.tool | jq -r '.disks."2000" | .label, .capacity' | paste - -

Hard disk 1 52143587328

Now I have this 2000 part of a variable. Like i=2000 And jq like

jq -r '.disks."$i" 


null null

I am unable to figure out how to pass this 2000 via a variable

1 Answer 1

jq -r --arg i "$i" '.disks[$i] | [ .label, .capacity ] | @tsv'

This passes the shell variable i into the jq expression as the jq variable $i (which is a string). You use this as a key with [$i].

I've also removed the need to use paste by using @tsv on an array containing the two values you want in jq instead. This would produce tab-delimited output, as would your paste command.

You also should not need to pass the data through python -m json.tool (I don't see what benefit this has). If it was used for pretty-printing, then that is not needed for input into jq.

Looking at the VMware API documentation, the request to get information about a specific disk looks like


This makes me think that you should be using vm-22 in place of {vm} and 2000 (or "$i") in place of {disk}, vm-22 is the name of you virtual machine and 2000 is your disk identifier.

This should give you something like

    "backing": {
        "type": "enum",
        "vmdk_file": "string"
    "capacity": 0,
    "ide": {
        "master": false,
        "primary": false
    "label": "string",
    "sata": {
        "bus": 0,
        "unit": 0
    "scsi": {
        "bus": 0,
        "unit": 0
    "type": "enum"

as a response.

You can parse the label and capacity out from this as a tab-delimited list, using

jq -r '[ .label, .capacity ] | @tsv'
  • The python -m json.tool was there for me to interpret some output nicely in pretty print. Wont actually exist in the script...Though the above command returns jq: error: syntax error, unexpected '[', expecting FORMAT or QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1: .disks.[$i] | [ .label, .capacity] | @tsv
    – Suhas
    Aug 20, 2021 at 9:21
  • jq -r --arg i "$j" '.disks | .[$i] | [.label, .capacity]|@tsv' This worked Even this jq -r --arg i "$i" '.disks | .[$i] | [.label, .capacity]|@tsv'
    – Suhas
    Aug 20, 2021 at 9:26
  • @Suhas Sorry, fixed now. No dot before [$i]. You can pretty print with jq . (in place of that python I mean).
    – Kusalananda
    Aug 20, 2021 at 9:34
  • What's the point of fiddling about with jq instead of just using sed or any other standard tool? Aug 20, 2021 at 10:35
  • 5
    @Kitsos JSON is not a line-oriented document format (so whitespace, including newlines, between values are not significant), and it may contain specially encoded values that may need decoding (if reading) or encoding (if writing). sed can definitely cope with all of this, but it would mean having to implement a JSON parser in sed to do it correctly. It is more convenient, safer, and makes for code that is easy to maintain to use jq.
    – Kusalananda
    Aug 20, 2021 at 10:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .