Code Monkey home page Code Monkey logo

json-to-csv's Introduction

JSON To CSV Converter

This is a modified version of json-to-csv project.

This code can be used for generating a flat CSV file from a list of JSON Objects. The JSONFlattener will create list of key-value pairs for the generated JSON. The CSVWriter would write the key value pairs to the specified file.

Usage:

1 - Using a JSON String:

  • JSON string:
{
	"firstName": "Brahim",
	"lastName": "Arkni"
}
  • JAVA code:
/*
 * Parse a JSON String and convert it to CSV
 */
List<Map<String, String>> flatJson = JSONFlattener.parseJson(jsonString);
// Using the default separator ','
// If you want to use an other separator like ';' or '\t' use
// CSVWriter.getCSV(flatJSON, separator) method
CSVWriter.writeToFile(CSVWriter.getCSV(flatJson), "files/sample_string.csv");
  • CSV output:
lastName,firstname
Arkni,Brahim

2 - Using a JSON file:

  • JSON file:

considering the file simple.json in /files directory, contains the following JSON

[
    {
        "studentName": "Foo",
        "Age": "12",
        "subjects": [
            {
                "name": "English",
                "marks": "40"
            },
            {
                "name": "History",
                "marks": "50"
            }
        ]
    },
    {
        "studentName": "Bar",
        "Age": "12",
        "subjects": [
            {
                "name": "English",
                "marks": "40"
            },
            {
                "name": "History",
                "marks": "50"
            },
            {
                "name": "Science",
                "marks": "40"
            }
        ]
    },
    {
        "studentName": "Baz",
        "Age": "12",
        "subjects": []
    }
]
  • JAVA code:
/*
 *  Parse a JSON File and convert it to CSV
 */
// There is 2 methods to parse the JSON file
// 1- JSONFlattener#parseJson(File file):
//    This will use the default encoding UTF-8 to parse the given file.
// 2- JSONFlattener#parseJson(File file, String encoding):
//    This will parse the file using the specified character encoding.
flatJson = JSONFlattener.parseJson(new File("files/simple.json"), "UTF-8");
// Using ';' as separator
CSVWriter.writeToFile(CSVWriter.getCSV(flatJson, ";"), "files/sample_file.csv");
Age;subjects[1].marks;subjects[1].name;subjects[2].marks;subjects[2].name;studentName;subjects[3].marks;subjects[3].name
12;40;English;50;History;Foo;;
12;40;English;50;History;Bar;40;Science
12;;;;;Baz;;

3 - Using a JSON returned from a URL:

  • JAVA code:
/*
 *  Parse JSON from URL and convert it to CSV
 */
// There is 2 methods to parse a JSON returned from a URI
// 1- JSONFlattener#parseJson(URI uri):
//    This will use the default encoding UTF-8 to parse the JSON returned from the given uri.
// 2- JSONFlattener#parseJson(URI uri, String encoding):
//    This will parse the JSON returned from the uri using the specified character encoding.
flatJson = JSONFlattener.parseJson(new URI("http://echo.jsontest.com/firstname/Brahim/lastName/Arkni"));
// Using '\t' as separator
CSVWriter.writeToFile(CSVWriter.getCSV(flatJson, "\t"), "files/sample_uri.csv");
  • JSON:

for this example, I used the web service echo.jsontest.com to echo a JSON object like the following

{
	"firstName": "Brahim",
	"lastName": "Arkni"
}
lastName	firstname
Arkni		Brahim

N.B:

  • The column names would dynamically be generated based on the keys in the JSON object.
  • The order of the JSON keys will not preserved during JSON conversion to CSV, See this StackOverFlow question for more information.

The sample output files can be seen here.

Licence

See the LICENCE file.

json-to-csv's People

Contributors

arkni avatar brtkca avatar

Watchers

James Cloos avatar Natsu Nopanut avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.