Code Monkey home page Code Monkey logo

json-csv's Introduction

JSON to CSV and CSV to JSON Converter

Latest Version on Packagist Software License Build Status Total Downloads

The most basic CSV to JSON and JSON to CSV converter library in PHP without any dependencies.

Install

Via Composer

$ composer require ozdemirburak/json-csv

Usage

JSON to CSV Converter

use OzdemirBurak\JsonCsv\File\Json;

// JSON to CSV
$json = new Json(__DIR__ . '/above.json');
// To convert JSON to CSV string
$csvString = $json->convert();
// To convert JSON to CSV and save
$json->convertAndSave(__DIR__ . '/above.csv');
// To convert JSON to CSV and force download on browser
$json->convertAndDownload();

Assume that the input JSON is something like below.

[
  {
    "name": {
      "common": "Turkey",
      "official": "Republic of Turkey",
      "native": "T\u00fcrkiye"
    },
    "area": 783562,
    "latlng": [39, 35]
  },
  {
    "name": {
      "common": "Israel",
      "official": "State of Israel",
      "native": "\u05d9\u05e9\u05e8\u05d0\u05dc"
    },
    "area": 20770,
    "latlng": [31.30, 34.45]
  }
]

After the conversion, the resulting CSV data will look like below.

name_common name_official name_native area latlng_0 latlng_1
Turkey Republic of Turkey Türkiye 783562 39 35
Israel State of Israel ישראל 20770 31.3 34.45

CSV to JSON Converter

use OzdemirBurak\JsonCsv\File\Csv;

// CSV to JSON
$csv = new Csv(__DIR__ . '/below.csv');
$csv->setConversionKey('options', JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
// To convert CSV to JSON string
$jsonString = $csv->convert();
// To convert CSV to JSON and save
$csv->convertAndSave(__DIR__ . '/below.json');
// To convert CSV to JSON and force download on browser
$csv->convertAndDownload();

Assume that the input CSV file is something like below.

SepalLength SepalWidth PetalLength PetalWidth Name
5.1 3.5 1.4 0.2 Iris-setosa
7.0 3.2 4.7 1.4 Iris-versicolor
6.3 3.3 6.0 2.5 Iris-virginica

After the conversion, the resulting JSON data will look like below.

[
  {
    "SepalLength": "5.1",
    "SepalWidth": "3.5",
    "PetalLength": "1.4",
    "PetalWidth": "0.2",
    "Name": "Iris-setosa"
  },
  {
    "SepalLength": "7.0",
    "SepalWidth": "3.2",
    "PetalLength": "4.7",
    "PetalWidth": "1.4",
    "Name": "Iris-versicolor"
  },
  {
    "SepalLength": "6.3",
    "SepalWidth": "3.3",
    "PetalLength": "6.0",
    "PetalWidth": "2.5",
    "Name": "Iris-virginica"
  }
]

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Known Issues

Currently, it is assumed that each object shares the same properties while converting JSON to CSV. So if one object has a property that the other one does not have, then it will be a major problem.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

json-csv's People

Contributors

ozdemirburak avatar

Watchers

James Cloos 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.