Changelog

Ruby Icon changelog.com

csonv.js: Fetch and transform CSV data into JSON

logged by @logbot 2011-06-24T14:30:58Z permalink #ruby

Since it’s typed, human readable, and supported darn near everywhere, JSON is the new hotness for data transport formats. Unfortunately, many systems don’t expose a JSON API. Relational data is often represented in the tried and true CSV format.

Paul Engel has introduced CSONV.js, a JavaScript library that can consume remote CSV data and transform it to JSON, a more client-side developer friendly format.

Consider the following delimited files:

# books.csv
id;name;author
integer;string;authors:1
1;To Kill an Angry Bird;1
2;The Rabbit;2
3;Parslet;3
4;The Lord of the Things;2
5;The Michelangelo Code;4

# authors.csv
id;name;written_books
integer;string;books:n:author
1;Harper Lee;
2;JRR Tolkien;
3;William Shakespeare;
4;Dan Brown;

CSONV.js can transform these two relational sources into the following JSON:

[
  {
    "id": 1,
    "name": "To Kill an Angry Bird",
    "author": {
      "id": 1,
      "name": "Harper Lee"
    }
  },
  {
    "id": 2,
    "name": "The Rabbit",
    "author": {
      "id": 2,
      "name": "JRR Tolkien"
    }
  },
  {
    "id": 3,
    "name": "Parslet",
    "author": {
      "id": 3,
      "name": "William Shakespeare"
    }
  },
  {
    "id": 4,
    "name": "The Lord of the Things",
    "author": {
      "id": 2,
      "name": "JRR Tolkien"
    }
  },
  {
    "id": 5,
    "name": "The Michelangelo Code",
    "author": {
      "id": 4,
      "name": "Dan Brown"
    }
  }
]

Be sure and check out the project on GitHub for complete feature list and advanced usage info.

[Source on GitHub] [Homepage]

0:00 / 0:00