Home > Back-end >  jq: error (at <stdin>:125): Cannot index array with string "DefId"
jq: error (at <stdin>:125): Cannot index array with string "DefId"

Time:12-19

I have below json data. Need help in parsing it.

{
  "e4624072-a9a2-4181-9649-550b9cfeb7cd||220000d7f738801": {
    "List": [
      {
        "Insts": [
          {
            "DefId": "A1",
            "data1": 1073741824,
            "data2": 0,
            "data3": 0
          }
        ]
      }
    ],
    "name": [
      "AMIT||220000d7f728801"
    ],
    "id": "e4624072-a9a2-4181-9649-550b9cfeb7cd||220000d7f738801",
    "endTime": 96285337200000
  },
  "0b1141b2-c2de-47c9-aa0c-2742f92b63f2||220000d7f738801": {
    "List": [
      {
        "Insts": [
          {
            "DefId": "B1",
            "data1": 5368709120,
            "data2": 5368709120,
            "data3": 5368709120
          }
        ]
      },
      {
        "edigest": "MV6shIv5NE5vWkc0cx6Q/JTwid4=",
        "csDefRef": "BoostYES_B1"
      },
      {
        "edigest": "MV6shIv5NE5vWkc0cx6Q/JTwid4=",
        "csDefRef": "BoostOff_B1"
      }
    ],
    "name": [
      "AMIT||220000d7f728801"
    ],
    "id": "0b1141b2-c2de-47c9-aa0c-2742f92b63f2||220000d7f738801",
    "lifeCycle": 0
  },
  "23e529f9-b2f3-4730-9b28-4ee05ca678b6||220000d7f738801": {
    "List": [
      {
        "Insts": [
          {
            "DefId": "A2",
            "data1": 1073741824,
            "data2": 0,
            "data3": 0,
            "lastUpdateTime": 1619541451476,
            "origInitialVal": 1073741824
          }
        ]
      }
    ],
    "name": [
      "AMIT||220000d7f728802"
    ],
    "id": "23e529f9-b2f3-4730-9b28-4ee05ca678b6||220000d7f738801",
    "endTime": 96285337200000,
    "lifeCycle": 0
  },
  "66b2229b-2c16-4d54-b2a8-fcc1baeaf51c||220000d7f738801": {
    "List": [
      {
        "Insts": [
          {
            "DefId": "B2",
            "data1": 10737418240,
            "data2": 10737418240,
            "data3": 10737418240,
            "lastUpdateTime": 1637766239807,
            "origInitialVal": 10737418240
          }
        ]
      },
      {
        "edigest": "MV6shIv5NE5vWkc0cx6Q/JTwid4=",
        "csDefRef": "RMN_B2"
      }
    ],
    "name": [
      "AMIT||220000d7f728801"
    ],
    "startTime": 1637766000000,
    "id": "66b2229b-2c16-4d54-b2a8-fcc1baeaf51c||220000d7f738801",
    "endTime": 1637852400000,
    "lifeCycle": 0
  },
  "b896eb1b-d6b0-432b-8925-af17431c0f3e||220000d7f738801": {
    "List": [
      {
        "Insts": [
          {
            "DefId": "B3",
            "data1": 2147483648,
            "data2": 2147483648,
            "data3": 2147483648,
            "lastUpdateTime": 1635692405780
          }
        ]
      },
      {
        "edigest": "MV6shIv5NE5vWkc0cx6Q/JTwid4=",
        "csDefRef": "BoostYES_B3"
      },
      {
        "edigest": "MV6shIv5NE5vWkc0cx6Q/JTwid4=",
        "csDefRef": "BoostOffCS_B3"
      }
    ],
    "name": [
      "AMIT||220000d7f728801"
    ],
    "id": "b896eb1b-d6b0-432b-8925-af17431c0f3e||220000d7f738801",
    "lifeCycle": 0
  }
}

Required output is as below :

AMIT||220000d7f728801,A1,1073741824,0,0
AMIT||220000d7f728801,B1,5368709120,5368709120,5368709120
AMIT||220000d7f728801,A2,1073741824,0,0
AMIT||220000d7f728801,B2,10737418240,10737418240,10737418240
AMIT||220000d7f728801,B3,2147483648,2147483648,2147483648

I tried to execute below jq to start with but it giving as mentioned in title. This might be due to DefId is not present at all the places. Similarly for data 1, data2 and data3.

jq -r '.[] | ."name", ."List"[]."Insts"."DefId"'

What is the right jq command in order to get the correct output?

CodePudding user response:

Unfortunately you say nothing about constraints (guaranteed existence of fields, lengths of arrays, etc.) regarding your data structure. Therefore, simply based on what you have provided, this will extract the contents into your desired output format:

jq -r '.[] | [.name[0] , (.List[0].Insts[0] | .DefId, .data1, .data2, .data3)] | join(",")' 
AMIT||220000d7f728801,A1,1073741824,0,0
AMIT||220000d7f728801,B1,5368709120,5368709120,5368709120
AMIT||220000d7f728801,A2,1073741824,0,0
AMIT||220000d7f728801,B2,10737418240,10737418240,10737418240
AMIT||220000d7f728801,B3,2147483648,2147483648,2147483648

Demo

CodePudding user response:

You can dynamically walk within List.Insts array without error by using

.List[].Insts | select( . != null ) for the returning values of to_entries[]

while it's straightforward to extract the value for the name key.

Then join the components of the formed array through use of wrapper square brackets such as

jq -r 'to_entries[] | .value | [ .name[] , (.List[].Insts | select( . != null ) | .[] | .DefId, "\(.data1)", "\(.data2)", "\(.data3)" ) ]  | join(",")'

Demo

  • Related