Home > Blockchain >  How do i get id from json with jq?
How do i get id from json with jq?

Time:01-14

i have json. How can I get the id whose attributes value is 0fda6bb8-4fc9-4463-9d26-af2d503cb19c ?

[
  {
    "id": "c3b1516d-5b2c-4838-b5eb-77d94d634832",
    "versionId": "c3b1516d-5b2c-4838-b5eb-77d94d634832",
    "name": "выписка маленькая заявка с лендинга ИБ",
    "entityTypeName": "TestCases",
    "projectId": "6dfe2ace-dd40-4e36-b66e-4a655a855a2f",
    "sectionId": "bf7fbece-4fdf-466a-b041-2d830debc844",
    "isAutomated": false,
    "globalId": 264511,
    "duration": 300,
    "attributes": {
      "1be40893-5dad-4b37-b70d-b830c4bd273f": "0fda6bb8-4fc9-4463-9d26-af2d503cb19c",
      "f4b408ae-5418-4a8d-99d9-4a67cb34870b": "fa000fb2-375d-4eb5-901c-fb5df30785ad"
    },
    "createdById": "995b1f08-cc65-409c-aa1c-a16c82dabf1d",
    "modifiedById": "995b1f08-cc65-409c-aa1c-a16c82dabf1d",
    "createdDate": "2022-10-12T00:22:43.544Z",
    "modifiedDate": "2022-10-12T00:22:43.544Z",
    "state": "NeedsWork",
    "priority": "Medium",
    "isDeleted": false,
    "tagNames": [
      "master"
    ],
    "iterations": []
  },
  {
    "id": "ec423701-f2a8-4667-8459-939a6e079941",
    "versionId": "0dfe176e-b172-47ae-8049-e6974086d497",
    "name": "[iOS] СБПэй фичатоглы. Fts.SBPay.Settings выключен Fts.C2B.Settings.Subscriptions включен",
    "entityTypeName": "TestCases",
    "projectId": "6dfe2ace-dd40-4e36-b66e-4a655a855a2f",
    "sectionId": "8626c9f5-a5aa-4584-bbca-e9cd60369a5e",
    "isAutomated": false,
    "globalId": 402437,
    "duration": 300,
    "attributes": {
      "1be40893-5dad-4b37-b70d-b830c4bd273f": "b52bfc88-9b13-41e1-8b4c-098ebfa673e0",
      "240b7589-9461-44dc-8b13-361132877c50": "cfd99bad-fb3f-43fe-be8a-cb745f2d4c78",
      "6639eb1a-1335-44ec-ba8b-c3c52bff9e79": "ed3bc553-e873-472f-8dc1-7f2720ad457d",
      "9ae36ef5-ca0e-4273-bb39-aedf289a119d": "6687017f-138b-4d75-91bd-c6465f1f5331",
      "b862c3ee-55eb-486f-8125-a7a034d69340": "IBANK5-37207",
      "f4b408ae-5418-4a8d-99d9-4a67cb34870b": "36dc55ac-359c-4312-9b1a-646ad5fd5aa9"
    },
    "createdById": "11a30c8b-73e2-4233-bbf5-7cc41556d3e0",
    "modifiedById": "11a30c8b-73e2-4233-bbf5-7cc41556d3e0",
    "createdDate": "2022-11-01T12:05:56.821Z",
    "modifiedDate": "2022-11-02T14:16:55.246Z",
    "state": "Ready",
    "priority": "Medium",
    "isDeleted": false,
    "tagNames": [],
    "iterations": []
  }
]

I tried using

cat new2.xml |  jq '.'  |  jq '.[] |  select(."1be40893-5dad-4b37-b70d-b830c4bd273f" | index("0fda6bb8-4fc9-4463-9d26-af2d503cb19c")) |  .[] .id'

but the search returns nothing

CodePudding user response:

You could select on .attributes[] and display the id field only:

jq '.[] | select(.attributes[] == "0fda6bb8-4fc9-4463-9d26-af2d503cb19c").id'

Output:

"c3b1516d-5b2c-4838-b5eb-77d94d634832"

With the input given, you'd get the same result with the more specific:

jq '.[] | select(.attributes["1be40893-5dad-4b37-b70d-b830c4bd273f"] == "0fda6bb8-4fc9-4463-9d26-af2d503cb19c").id'

(because there's only one attribute Key with the Value "0fda6bb8-4fc9-4463-9d26-af2d503cb19c" in your example)

  • Related