JSON2CSV

Last week I’ve needed a utility to convert a file containing json data to csv. I found many online solutions, but for some weird reason they didn’t support nested objects and arrays. So I wrote one, this time in python. Grab it here - Github repository.

Usage

1
python json2csv.py "input_file.json" "output_file.csv"

If you pass in the following json file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[
  {
      "id": 1,
      "name": {
          "first": "john",
          "last": "johnson"
      },
      "age": 27,
      "languages": [ "c#", "vb", "python" ]
   },
   {
      "id": 2,
      "name": {
          "first": "scott",
          "middle": "scottster",
          "last": "scottson"
      },
      "age": 29,
      "languages": [ "objective-c", "c++" ]
   }
]

You’ll get the following csv file:

1
2
3
age,  id,    languages_0,    languages_1,    languages_2,    name_first, name_last,  name_middle
27,   1,      c#,             vb,             python,         john,       johnson,
29,   2,      objective-c,    c++,            ,               scott,      scottson,   scottster

Comments