Socket
Socket
Sign inDemoInstall

github.com/thorborn-dev/go-csv-to-json

Package Overview
Dependencies
4
Alerts
File Explorer

Install Socket

Detect and block malicious and high-risk dependencies

Install

    github.com/thorborn-dev/go-csv-to-json


Version published

Readme

Source

Go: csv-to-json

Convert CSV to (nested) JSON.

Table of Contents

  • Usage
  • Examples
  • Currenty not supported CSV input

Usage

package main

import (
    "fmt"

    "github.com/thorborn-dev/go-csv-to-json"
)

func main() {
    csv, err := csv.ReadCSV("path/to/csv")
    if err != nil {
        panic(err)
    }
    json, err := csv.ToJSON()
    if err != nil {
        panic(err)
    }
    fmt.Println(json)
}

Examples

Basic Data Types

CSV Input:

name,age,registered,verified
John,25,TRUE,false

JSON Output:

[
  {
    "name": "John",
    "age": 25,
    "registered": true,
    "verified": false
  }
]

Objects

CSV Input:

user.firstname,user.lastname
John,Doe

JSON Output:

[
  {
    "user": {
      "firstname": "John",
      "lastname": "Doe"
    }
  }
]

Arrays

Arrays of Basic Data Types

CSV Input:

names[0],names[1]
John,Jane

JSON Output:

[
  {
    "names": ["John", "Jane"]
  }
]

Arrays of Objects

CSV Input:

users[0].firstname,users[0].lastname,users[1].firstname,users[1].lastname
John,Doe,Jane,Doe

JSON Output:

[
  {
    "users": [
      {
        "firstname": "John",
        "lastname": "Doe"
      },
      {
        "firstname": "Jane",
        "lastname": "Doe"
      }
    ]
  }
]

Currently not supported CSV input

Arrays of Arrays

CSV Input:

users[0][0],users[0][1]
John,Jane

Expected JSON Output:

[
  {
    "users": [["John", "Jane"]]
  }
]

License

MIT

FAQs

Last updated on 30 Dec 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc