# Convert a csv file column to array of json

**URL:** https://discuss.elastic.co/t/convert-a-csv-file-column-to-array-of-json/344584
**Category:** Logstash
**Created:** [October 8, 2023, 4:49am UTC](https://discuss.elastic.co/t/convert-a-csv-file-column-to-array-of-json/344584 "2023-10-08T04:49:26Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Sudharsan\_Aravind](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sudharsan_aravind/32/126333_2.png) [@Sudharsan\_Aravind](https://discuss.elastic.co/u/Sudharsan_Aravind)
#### Post date: [October 8, 2023, 4:49am UTC](https://discuss.elastic.co/t/convert-a-csv-file-column-to-array-of-json/344584/1 "2023-10-08T04:49:26Z")

</div>

I have a csv file, where there is this one column with array of json.

```auto
column1, column2, array_json_col, column4, ...
item1, item2, "[{'type': 'StillImage', 'format': 'image/jpeg', 'url',: 'https://sample.url'}, {'type': 'StillImage', 'format': 'image/jpeg', 'url',: 'https://sample.url'}]", item4, ...

```

When I try,

```auto
input {
  file {
    path => "/usr/share/logstash/ingest_data/base_dataset_v2.csv"
    start_position =>"beginning"
    sincedb_path => "/usr/share/logstash/data/plugins/inputs/file/.sincedb"
  }
}

filter {
    csv {
        separator => ","
        columns => ["column1", "column2", "array_json_col", "column4"]
    }
}

filter {
     ruby {
        code => "
          require 'json'
          media = event.get('media')
          media = JSON.parse(media)
          event.set('media', media)
        "
    }
}

output {
  elasticsearch {
    index => "idx_001"
    hosts=> ["http://elasticsearch:9200"]
  }
}

```

I'm unable to convert the `array_json_col` to array of json type, and the column is rather stored as a string like, `"[{'type': 'StillImage', 'format': 'image/jpeg', 'url',: 'https://sample.url'}, {'type': 'StillImage', 'format': 'image/jpeg', 'url',: 'https://sample.url'}]"`.

Can someone help me with the right logstash.conf to solve this issue?

---

<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: [October 8, 2023, 8:45pm UTC](https://discuss.elastic.co/t/convert-a-csv-file-column-to-array-of-json/344584/2 "2023-10-08T20:45:16Z")

</div>

You could try

```
    mutate { gsub => ["message", ', "', ',"'] }
    csv {
        separator => ","
        columns => ["column1", "column2", "array_json_col", "column4"]
    }
    mutate { gsub => ["array_json_col", "'", '"', array_json_col, '",:', '":'] }
    json { source => "array_json_col" target => "media" }

```

---

<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: [November 5, 2023, 8:46pm UTC](https://discuss.elastic.co/t/convert-a-csv-file-column-to-array-of-json/344584/3 "2023-11-05T20:46:14Z")

</div>

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