Home > Enterprise >  Get value of a key contained in an array with several JSON by command line
Get value of a key contained in an array with several JSON by command line

Time:02-27

I have an array that contains many json inside. I want to search within that array for the uuid value that is in the same position as the json that contains the name:

"name":"120GB"

I need something that actually looks inside the arrays and gets the value of uuid. I don't know if this will be possible through the Linux command line. I've been thinking about it for a long time and I don't know how to get my hands on it.

This is the array, sometimes the json containing the "name:120GB" is in the first place, sometimes in the second, etc.

Here the array:

  [
  {
    "uuid": "c70c1627-71ec-46c8-8ce1-43055adcb606",
    "name": "DESCARGAS",
    "comment": "",
    "mntentref": "7be7b356-177a-4436-802a-2e7b785d52b6",
    "reldirpath": "DESCARGAS/",
    "privileges": "",
    "_used": true,
    "device": "6TBNAS",
    "description": "DESCARGAS [on 6TBNAS, DESCARGAS/]",
    "mntent": {
      "devicefile": "/dev/sdc1",
      "fsname": "/dev/disk/by-uuid/c8175ed0-e2a8-473f-846c-36e5dc79e4bd",
      "dir": "/srv/dev-disk-by-uuid-c8175ed0-e2a8-473f-846c-36e5dc79e4bd",
      "type": "ext4",
      "posixacl": true
    }
  },
  {
    "uuid": "70593bb6-0876-436c-bdd7-3b401fb55d50",
    "name": "MUSICA",
    "comment": "",
    "mntentref": "6c59b572-faf0-4682-85e8-ad92eaa6d34a",
    "reldirpath": "MUSICA/",
    "privileges": "",
    "_used": true,
    "device": "3TBNAS",
    "description": "MUSICA [on 3TBNAS, MUSICA/]",
    "mntent": {
      "devicefile": "/dev/sdb1",
      "fsname": "/dev/disk/by-uuid/2cac8ffa-bca4-4564-904f-cae65e56b650",
      "dir": "/srv/dev-disk-by-uuid-2cac8ffa-bca4-4564-904f-cae65e56b650",
      "type": "ext4",
      "posixacl": true
    }
  },
  {
    "uuid": "f0a0aa62-2a97-4cee-86d9-55961a675978",
    "name": "NINA",
    "comment": "",
    "mntentref": "6c59b572-faf0-4682-85e8-ad92eaa6d34a",
    "reldirpath": "NINA/",
    "privileges": "",
    "_used": true,
    "device": "3TBNAS",
    "description": "NINA [on 3TBNAS, NINA/]",
    "mntent": {
      "devicefile": "/dev/sdb1",
      "fsname": "/dev/disk/by-uuid/2cac8ffa-bca4-4564-904f-cae65e56b650",
      "dir": "/srv/dev-disk-by-uuid-2cac8ffa-bca4-4564-904f-cae65e56b650",
      "type": "ext4",
      "posixacl": true
    }
  },
  {
    "uuid": "7fe35e3b-8cbe-43f3-ad72-d0710ce6e0e7",
    "name": "COMPARTIR",
    "comment": "",
    "mntentref": "7be7b356-177a-4436-802a-2e7b785d52b6",
    "reldirpath": "COMPARTIR/",
    "privileges": "",
    "_used": true,
    "device": "6TBNAS",
    "description": "COMPARTIR [on 6TBNAS, COMPARTIR/]",
    "mntent": {
      "devicefile": "/dev/sdc1",
      "fsname": "/dev/disk/by-uuid/c8175ed0-e2a8-473f-846c-36e5dc79e4bd",
      "dir": "/srv/dev-disk-by-uuid-c8175ed0-e2a8-473f-846c-36e5dc79e4bd",
      "type": "ext4",
      "posixacl": true
    }
  },
  {
    "uuid": "abbaca32-09a2-410b-9918-dd1d0ee66273",
    "name": "120GB",
    "comment": "",
    "mntentref": "1157e369-73a4-4c61-b8d2-7cbccc207a11",
    "reldirpath": "/",
    "privileges": "",
    "_used": false,
    "device": "120GB_ANTIGUO",
    "description": "120GB [on 120GB_ANTIGUO, /]",
    "mntent": {
      "devicefile": "/dev/sdd1",
      "fsname": "/dev/disk/by-uuid/269E8B449E8B0B8D",
      "dir": "/srv/dev-disk-by-uuid-269E8B449E8B0B8D",
      "type": "ntfs",
      "posixacl": false
    }
  }
]

How could I get the value of uuid? I hope I have explained myself well.

CodePudding user response:

You can extract the uuid using jq:

cat file | jq '.[] | select(.name == "120GB") | {uuid}' 

sample output:

{
  "uuid": "abbaca32-09a2-410b-9918-dd1d0ee66273"
}

Only the uuid:

cat file | jq '.[] | select(.name == "120GB") | {uuid}' |awk -F: '{print $2}'

sample output:

"abbaca32-09a2-410b-9918-dd1d0ee66273"

Or:

cat file | jq '.[] | select(.name == "120GB") | {uuid}' |awk -F'"' '{print $4}'

abbaca32-09a2-410b-9918-dd1d0ee66273
  • Related