Project

json2

0.0
No commit activity in last 3 years
No release in over 3 years
json2 takes a json file as input and outputs a clean csv.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.7
~> 0.13.0
>= 0
~> 10.0
~> 3.0
>= 0
 Project Readme

Json2

Json2 transforms a Json file into a Csv one.

Installation

Add this line to your application's Gemfile:

gem 'json2'

And then execute:

$ bundle

Or install it yourself as:

$ gem install json2

Usage

Example 1

Given the following Json file:

[
	{
		"color": "red",
		"value": "#f00"
	},
	{
		"color": "green",
		"value": "#0f0"
	},
	{
		"color": "blue",
		"value": "#00f"
	}
]

You could obtain a Csv with a header like that:

$ json2 colors-array.json 
color,value
red,#f00
green,#0f0
blue,#00f

Example 2

With this simpler kind of Json:

{
    "red":"#f00",
    "green":"#0f0",
    "blue":"#00f"
}

Here is the resulting Csv:

$ json2 colors3.json 
red,green,blue
#f00,#0f0,#00f

Example 3

Take a slightly more complicated Json file:

{
	"id": "0001",
	"type": "donut",
	"batters":
		{
			"batter":
				[
					{ "id": "1001", "type": "Regular" },
					{ "id": "1002", "type": "Chocolate" },
					{ "id": "1003", "type": "Blueberry" },
					{ "id": "1004", "type": "Devil's Food" }
				]
		},
	"topping":
		[
			{ "id": "5001", "type": "None" },
			{ "id": "5002", "type": "Glazed" },
			{ "id": "5005", "type": "Sugar" }
		]
}

Say you want to extract the batter stuff. Use the --path switch:

$ json2 --path='batters.batter' products.json 
id,type
1001,Regular
1002,Chocolate
1003,Blueberry
1004,Devil's Food

Example 4

Json don't always map very well with Csv. Sometimes there is simply no header information inside the Json:

{
    "Nom du parti": {
        "Abstention": [
            "Jean"
        ],
        "Non-votant": [],
        "Contre": [
            "Alice",
            "Georges"
        ],
        "Pour": [
            "Julie"
        ]
    }
}

In this case, you can use the --without-header switch:

$ json2 --without-header votes.json 
Nom du parti,Abstention,Jean
Nom du parti,Non-votant,
Nom du parti,Contre,Alice
Nom du parti,Contre,Georges
Nom du parti,Pour,Julie

Contributing

  1. Fork it ( https://github.com/[my-github-username]/json2/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

License

MIT

Questions and/or Comments

Feel free to email Xavier Nayrac with any questions, or contact me on twitter.