zemirco / json2csv-stream

Transform stream from json to csv
52 stars 17 forks source link

SyntaxError: Unexpected end of input #4

Closed mderazon closed 5 years ago

mderazon commented 10 years ago

Hi, very nice module ! Some of my data has nested objects in it, I got this exception thrown:

events.js:72
        throw er; // Unhandled 'error' event
              ^
SyntaxError: Unexpected end of input
    at Object.parse (native)
    at MyStream.writeLine (/Users/me/company-src/company-admin/node_modules/json2csv-stream/index.js:147:25)
    at MyStream._transform (/Users/me/company-src/company-admin/node_modules/json2csv-stream/index.js:97:12)
    at Transform._read (_stream_transform.js:179:10)
    at Transform._write (_stream_transform.js:167:12)
    at doWrite (_stream_writable.js:221:10)
    at writeOrBuffer (_stream_writable.js:211:5)
    at Writable.write (_stream_writable.js:180:11)
    at Stream.ondata (stream.js:51:26)
    at Stream.EventEmitter.emit (events.js:95:17)
    at drain (/Users/me/company-src/company-admin/node_modules/JSONStream/node_modules/through/index.js:35:16)
    at Stream.stream.queue.stream.push (/Users/me/company-src/company-admin/node_modules/JSONStream/node_modules/through/index.js:41:5)

Here's the line that goes into JSON.parse() right before it crashes

{
  "id":"shem@walla.com",
  "create_time":"2013-04-04T06:03:15.394Z",
  "facebook_id":"1584215606",
  "facebook_name":"Shira Shem",
  "facebook_first_name":"Shira",
  "facebook_last_name":"Shem",
  "facebook_link":"http://www.facebook.com/shira.shem",
  "facebook_username":"shira.shem",
  "facebook_gender":"female",
  "facebook_email":"shem@walla.com",
  "facebook_timezone":"3",
  "facebook_locale":"en_US",
  "facebook_verified":"true",
  "facebook_updated_time":"2013-02-26T09:31:23+0000",
  "facebook_picture_url":"https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc6/275966_1584215606_861_q.jpg",
  "apn_token":"6c18ce337bf234f63bef72cd424fbc3b766f880b3d3d",
  "_id":"515d17a3fc020",
  "push":{
    "push_PromotionPush1":{
      "id":"push_PromotionPush1",
      "message":"קצת טקסט בעברית",
      "title":"BLA CARD",
      "link":"",
      "timestamp":"2013-08-20T14:38:57.587Z"
    }

This is not a valid JSON object, the nested push object doesn't close properly. Looking at the data I get from the stream, I see that the data integrity is okay, so the problem might be that it breaks the JSON object in the middle of the line ?

Is the any way to tell json2csv to ignore nested objects ? (How does it deal with them anyway ? does it flatten them ?)

mderazon commented 10 years ago

btw, i'm curious to know how did you handle streaming all keys without knowing the keys in advance ? The keys have to go on the top of the file no ?

jwerre commented 10 years ago

+1 I'm having the same problem. How do we handle nested object/collections?

zemirco commented 10 years ago

close because deprecated: No time for maintenance

KGabson commented 6 years ago

How did you guys solve that issue ?

knownasilya commented 5 years ago

https://github.com/zemirco/json2csv now has a streams API, please use that module as this one is deprecated/unmaintained.