jsoncsv icon indicating copy to clipboard operation
jsoncsv copied to clipboard

Fix issues with JSON handling

Open alexbfree opened this issue 9 years ago • 0 comments

Hi, I found some issues using your code.

Here is my test data (stored as events.json):

{
  "events": [
    {
      "id": "000ukJXw7xzK8YDU",
      "time": "2015-01-31T17:52:36.282Z",
      "subject_id": "ASG0013qjf",
      "user_id": "1911627",
      "type": "login",
      "related_id": ""
    },
    {
      "id": "001FVORPsS8R8BMm",
      "time": "2015-02-10T16:24:11.127Z",
      "type": "logout",
      "related_id": ""
    },
    {
      "id": "zzwfDfCytBqmpTNf",
      "time": "2015-02-05T13:37:28.501Z",
      "subject_id": "ASG0013nl0",
      "user_id": "1186777",
      "type": "classify",
      "related_id": "c-1"
    }
  ]
}    

Here is my test script (pretty much like yours):

fs = require 'fs'
jsoncsv = require '/code/jsoncsv/lib/jsoncsv'

COLUMNS = ['time','subject_id','user_id','type','related_id']
data = fs.readFileSync('events.json').toString()

jsoncsv.parse data, COLUMNS, (err, row) -> 
  console.log row

When I ran your code as is, I experienced a number of issues:

  • The output came out as single characters instead of full strings. I think this is because you were assuming you already had an array of JSON objects, whereas in fact JSON format specifies that the array is contained within the first key of the main JSON object.
  • The loop count incrementer seemed to be in the wrong place.
  • You were performing JSON.stringify, but the example program already reads in data as a string, so this is not needed, and in fact stops the code from working.
  • There is a comma at the end of each line that should not be there.
  • You seem to have both CoffeeScript and JavaScript versions of the same code checked in. I tried to change them both to make them consistent.

With the changes contained in this pull request, I get the following output, which looks correct to me:

time,subject_id,user_id,type,related_id
2015-01-31T17:52:36.282Z,ASG0013qjf,1911627,login,
2015-02-10T16:24:11.127Z,logout,
2015-02-05T13:37:28.501Z,ASG0013nl0,1186777,classify,c-1

Hope you find my changes useful. I am not sure that the code would have worked on standard formatted JSON as implemented.

Thanks for implementing this, it saved me a lot of time.

alexbfree avatar Mar 17 '15 14:03 alexbfree