Home > OS >  Access a key outside of an array and store in a variable
Access a key outside of an array and store in a variable

Time:10-13

I have this bit of json:

    [{
        "id": 123,
        "name": "Ice",
        "categories": [{
            "products": [{
                    "id": "66642",
                    "display_name": "Ice A"
                },
                {
                    "id": "62466",
                    "display_name": "Ice B"
                },
                {
                    "id": "62466",
                    "display_name": "Ice B duplicate"
                }
            ]
        }]
    },
    {
        "id": 97,
        "name": "Cakes",
        "categories": [{
            "products": [{
                    "id": "14518",
                    "display_name": "Cake 1"
                },
                {
                    "id": "14105",
                    "display_name": "Cake 2"
                }
            ]
        }]
    }
]

I'm using jq 1.6 and I'm trying to get this output:

[{
  "id": "62466",
  "name": "Ice B",
  "category": "Ice"
},
{
  "id": "66642",
  "name": "Ice A",
  "category": "Ice"
},
{
  "id": "14105",
  "name": "Cake 2",
  "category": "Cakes"
},
{
  "id": "14518",
  "name": "Cake 1",
  "category": "Cakes"
}]

I've tried with this code:

.[].name as $x | map(.categories[].products[]) | unique_by(.id) | .[] | 
[{
    id: .id,
    name: .display_name,
    category: $x
}]

But I get this output:

[
  {
    "id": "14105",
    "name": "Cake 2",
    "category": "Ice"
  }
]
[
  {
    "id": "14518",
    "name": "Cake 1",
    "category": "Ice"
  }
]
[
  {
    "id": "62466",
    "name": "Ice B",
    "category": "Ice"
  }
]
[
  {
    "id": "66642",
    "name": "Ice A",
    "category": "Ice"
  }
]
[
  {
    "id": "14105",
    "name": "Cake 2",
    "category": "Cakes"
  }
]
[
  {
    "id": "14518",
    "name": "Cake 1",
    "category": "Cakes"
  }
]
[
  {
    "id": "62466",
    "name": "Ice B",
    "category": "Cakes"
  }
]
[
  {
    "id": "66642",
    "name": "Ice A",
    "category": "Cakes"
  }
]

I looks like as there are two names

Ice
Cakes

It's multiplying the output by two , one for each name instead of just returning the 4 products and I can't get the result into and array of objects. Here is a reproducible code in jqplay. Because there are some duplicate records, ie: products that have more than one category I need them to be unique. How can i fix this?

CodePudding user response:

Assigning the category name in a variable and then transforming each product with unique id:

map(
    .name as $category
    | .categories[].products
    | unique_by(.id)
    | .[]
    | { id, name: .display_name, $category }
)

CodePudding user response:

This map produces the 4 unique items.

jq 'map(
  (.categories[].products | unique_by(.id)[] | {id, name: .display_name})
    {category: .name}
)'
[
  {
    "id": "62466",
    "name": "Ice B",
    "category": "Ice"
  },
  {
    "id": "66642",
    "name": "Ice A",
    "category": "Ice"
  },
  {
    "id": "14105",
    "name": "Cake 2",
    "category": "Cakes"
  },
  {
    "id": "14518",
    "name": "Cake 1",
    "category": "Cakes"
  }
]

Demo

  • Related