# Parse JSON array with nested objects

**URL:** <https://discuss.elastic.co/t/parse-json-array-with-nested-objects/307770>\
**Category:** Logstash\
**Created:** [June 21, 2022, 2:46pm UTC](https://discuss.elastic.co/t/parse-json-array-with-nested-objects/307770 "2022-06-21T14:46:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![eliz](https://avatars.discourse-cdn.com/v4/letter/e/848f3c/32.png) [@eliz](https://discuss.elastic.co/u/eliz)\
**Post date:** [June 21, 2022, 2:46pm UTC](https://discuss.elastic.co/t/parse-json-array-with-nested-objects/307770/1 "2022-06-21T14:46:40Z")

</div>

Hi there! I am receiving an array of nested JSON elements and I would like to build a logic capable of transforming the following input

```auto
{
  "devices": [
    {
      "device": {
        "device_name": "printer_1",
        "other_field1": "abc",
        "other_field2": "def"
      },
      "category": {
        "category_name": "printers",
        "other_field3": "ghi",
        "other_field4": "jkl"
      },
      "device_id": "1234567890"
    },
    {
      "device": {
        "device_name": "printer_2",
        "other_field1": "abc",
        "other_field2": "def"
      },
      "category": {
        "category_name": "printers",
        "other_field3": "ghi",
        "other_field4": "jkl"
      },
      "device_id": "0987654321"
    },
    {
      "device": {
        "device_name": "laptop_1",
        "other_field1": "abc",
        "other_field2": "def"
      },
      "category": {
        "category_name": "laptops",
        "other_field3": "ghi",
        "other_field4": "jkl"
      },
      "device_id": "2468013579"
    }
  ]
}

```

in this output:

```auto
{
    "printers": ["printer_1", "printer_2"],
    "laptops": ["laptop_1"]
}

```

Thanks!

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [June 21, 2022, 5:03pm UTC](https://discuss.elastic.co/t/parse-json-array-with-nested-objects/307770/2 "2022-06-21T17:03:50Z")

</div>

You will need to use a ruby filter

```
    ruby {
        code => '
            devices = event.get("devices")
            if devices
                devices.each { |x|
                    dev = x["device"]["device_name"]
                    cat = x["category"]["category_name"]
                    catValue = event.get(cat)
                    if catValue
                        event.set(cat, catValue << dev)
                    else
                        event.set(cat, [dev])
                    end
                }
            end
        '
    }

```

will produce

```
  "printers" => [
    [0] "printer_1",
    [1] "printer_2"
],
   "laptops" => [
    [0] "laptop_1"
]

```

---

<div class="post-metadata">

**Author:** ![eliz](https://avatars.discourse-cdn.com/v4/letter/e/848f3c/32.png) [@eliz](https://discuss.elastic.co/u/eliz)\
**Post date:** [June 21, 2022, 6:37pm UTC](https://discuss.elastic.co/t/parse-json-array-with-nested-objects/307770/3 "2022-06-21T18:37:09Z")

</div>

Thanks, @Badger. That was exactly what I am looking for.  
Appreciate your help.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 19, 2022, 6:37pm UTC](https://discuss.elastic.co/t/parse-json-array-with-nested-objects/307770/4 "2022-07-19T18:37:59Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
