Skip to content

Latest commit

 

History

History
79 lines (60 loc) · 1.72 KB

JQ Transverse List as Map.md

File metadata and controls

79 lines (60 loc) · 1.72 KB

(jump to the answer)

I want to convert the Below JSON to Map. but its coming as a list.

{
  "_embedded": {
    "items": [
      {
         "order": 1,
        "technical": {
          "id": 1,
          "_links": {
            "self": {
              "href": "/profile/api/v3/service/40/technical"
            }
          }
        }
      },
       {
        "order": 2,
        "technical": {
          "id": 1,
          "_links": {
            "self": {
              "href": "/profile/api/v3/service/40/technical"
            }
          }
        }
      }
    ]
  }
}

Expected format:

{
  "1": "/profile/api/v3/service/40/technical",
  "2": "/profile/api/v3/service/40/technical"
}

But i am getting :::

[
  {
    "1": "/profile/api/v3/service/40/technical"
  },
  {
    "2": "/profile/api/v3/service/40/technical"
  }
]

JQ Query:::

._embedded.items | map({(.order| tostring ) : .technical._links.self.href} )

Please help, Thanks in Advance..

Code Snippet - https://jqplay.org/s/cEzh5_LimP

A:

with jtc: walk each order and memorize its value then find respective href and transform into the required format:

bash $ <sample.json jtc -w'<order>l:<O>v[-1]<href>l' -T'{"{O}":{{}}}' -lljj
{
   "1": "/profile/api/v3/service/40/technical",
   "2": "/profile/api/v3/service/40/technical"
}
bash $