Coder Social home page Coder Social 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 dsambugaro avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

json-to-csv's Issues

Design Patterns

For educational reasons in this repository identify 2 design patterns that can be applied.

Chain of Responsibility
The JSON to CSV conversion job can be modeled through a chain of responsibilities, where each type of conversion will evaluate whether or not it can convert the file.

public class Main {
public static void main(String[] args) throws Exception {
/*
* Parse a JSON String and convert it to CSV
*/
List<Map<String, String>> flatJson = JSONFlattener.parseJson(jsonString());
// Using the default separator ','
CSVWriter.writeToFile(CSVWriter.getCSV(flatJson), "files/sample_string.csv");
/*
* Parse a JSON File and convert it to CSV
*/
flatJson = JSONFlattener.parseJson(new File("files/simple.json"), "UTF-8");
// Using ';' as separator
CSVWriter.writeToFile(CSVWriter.getCSV(flatJson, ";"), "files/sample_file.csv");
/*
* Parse JSON from URL and convert it to CSV
*/
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");
/*
* Parse a Large JSON File and convert it to CSV
*/
flatJson = JSONFlattener.parseJson(new File("files/sample_large.json"), "UTF-8");
// Using ';' as separator
Set<String> header = CSVWriter.collectOrderedHeaders(flatJson);
// the intention is generate a csv file with specific headers - not all
CSVWriter.writeLargeFile(flatJson, ";", "files/sample_largeFile.csv", header);
}
private static String jsonString() {
return "[" +
" {" +
" \"studentName\": \"Foo\"," +
" \"Age\": \"12\"," +
" \"subjects\": [" +
" {" +
" \"name\": \"English\"," +
" \"marks\": \"40\"" +
" }," +
" {" +
" \"name\": \"History\"," +
" \"marks\": \"50\"" +
" }" +
" ]" +
" }" +
"]";
}
}

Remaining as follows

image
image
image
image
image
image

UML class diagram
image

Facade
Following the previous pattern as a subsystem, the facade pattern can be used to give easy access to the entire chain of handlers.
image

Remaining as follows

image

UML class diagram

image

Code Smelles

Hello, for academic reasons my group and I found a code smell that is ["comments"
aviso1

Append To CSV

hi,, i get java heap memory when i create array list to make "List<Map<String, String>> flatJson = JSONFlattener.parseJson(listDoc.toString());" because i have hundreds row of data.
so how i can append it one by one using "CSVWriter.writeToFile(CSVWriter.getCSV(flatJson, ";"), "");" on exist file???

simple.json is not found

[INFO] [2016-11-09 08:41:22] JSONFlattener:130 - Handle the JSON String as JSON Array
[ERROR] [2016-11-09 08:41:22] JSONFlattener:107 - JsonFlattener#ParseJson(file, encoding) IOException:
java.io.FileNotFoundException: File 'files\simple.json' does not exist
at org.apache.commons.io.FileUtils.openInputStream(FileUtils.java:299)
at org.apache.commons.io.FileUtils.readFileToString(FileUtils.java:1711)
at org.apache.commons.io.FileUtils.readFileToString(FileUtils.java:1734)
at org.jsontocsv.parser.JSONFlattener.parseJson(JSONFlattener.java:104)
at org.jsontocsv.Main.main(Main.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Exception in thread "main" java.lang.NullPointerException
at org.jsontocsv.writer.CSVWriter.collectHeaders(CSVWriter.java:110)
at org.jsontocsv.writer.CSVWriter.getCSV(CSVWriter.java:58)
at org.jsontocsv.Main.main(Main.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

JAVA.NIO. Package not found

How to pull the JAVA.NIO Package in this projects, i tried a lot but did not get solution could you explain me please. Thanks in advance.

Importancia y Uso del Patrón Adapter en el Código Base

Actualmente, estamos analizando el diseño y la arquitectura del código base para identificar oportunidades de mejora y optimización. Después de un análisis exhaustivo, hemos notado una situación en la que la implementación del patrón de diseño Adapter podría proporcionar beneficios significativos para el sistema.

Propuesta:
La implementación del patrón Adapter permitirá adaptar las interfaces de las bibliotecas y componentes externos a una interfaz común, lo que simplificará su uso y aumentará la flexibilidad de nuestro sistema. Al utilizar el Adapter, podemos:

Mejorar la Interoperabilidad: El Adapter nos permitirá trabajar con componentes externos incompatibles a través de una interfaz común. Esto mejorará la colaboración entre diferentes partes del sistema y aumentará la coherencia en el código.

Facilitar la Mantenibilidad: Al encapsular la adaptación en las clases Adapter, podremos gestionar los cambios y actualizaciones en las bibliotecas externas con mayor facilidad. No tendremos que realizar modificaciones extensas en todo el sistema cada vez que haya cambios en las interfaces de las bibliotecas.

Promover la Reutilización: Al implementar el Adapter, crearemos una capa de adaptación independiente que puede reutilizarse en diferentes partes del sistema. Esto evitará la duplicación de código y mejorará la coherencia en toda la base de código.

Acción Propuesta:
Recomendamos llevar a cabo una revisión detallada de las áreas en las que interactuamos con componentes externos incompatibles. Esta refactorización ayudará a simplificar el código, reducir la complejidad y mejorar la mantenibilidad en el futuro.

Impacto Esperado:
La implementación del patrón Adapter traerá claros beneficios en términos de coherencia, mantenibilidad y reutilización de código. Además, facilitará la integración de nuevas bibliotecas y componentes en el futuro, lo que acelerará el desarrollo y permitirá una evolución más ágil del sistema.

Chain of responsibility

El código de las diferentes formas para convertir los archivos se encuentra originalmente en
la clase Main. se debe crear una clase abstracta que tenga sus clases concretas para que funcionen como manejadores, por si se quiere transformar un archivo JSON, pasará por cada manejador y verificará si es capaz
de convertirlo a formato CSV, caso contrario pasará al siguiente, hasta lograr convertir el
documento.
figura

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.