{
  "id": 2144,
  "title": "Reading enormous JSON files",
  "url": "/competitions/cprod1/discussion/2144",
  "author_name": "",
  "post_date": "2012-07-07T00:20:50.287Z",
  "votes": null,
  "comment_count": 8,
  "views": 6998,
  "content": "<p>Does anyone have any tips for working with JSON files of this size? Are you doing some homebrew trickery to delimit them before reading? I can't imagine that it's possible to load the training-anotated-text.json file into memory, yet, because of the structure\r\n of JSON files, you can't deal with it in a stream like you can a large CSV.</p>\r\n<p>I've tried loading just the products file with rjson and ~10 minutes later and to RAM left, I'm still waiting.</p>",
  "messages": [
    {
      "id": "12180",
      "postDate": "07/07/2012 00:20:50",
      "content": "<p>Does anyone have any tips for working with JSON files of this size? Are you doing some homebrew trickery to delimit them before reading? I can't imagine that it's possible to load the training-anotated-text.json file into memory, yet, because of the structure\r\n of JSON files, you can't deal with it in a stream like you can a large CSV.</p>\r\n<p>I've tried loading just the products file with rjson and ~10 minutes later and to RAM left, I'm still waiting.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "12189",
      "postDate": "07/07/2012 06:21:19",
      "content": "<p>I'd start by reading some part of the file as a stream and then dumping into a &quot;sample&quot; file, just to figure out what's going on. While I see &nbsp;your point, I can't believe there's no way around loading the whole thing into memory - I mean, 0.7gb zipped?&nbsp;</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "12190",
      "postDate": "07/07/2012 07:02:42",
      "content": "<p>Yeah, I know there has to be a way. But it seems like it would especially be easier if the top-level element was an array rather than an object.</p>\r\n<p>As far as the data structure is concerned, it defines it as a monolithic whole, and splitting out the parts seems like a trick task to me. Not just as easy as pattern matching to delimit array elements.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "12219",
      "postDate": "07/09/2012 11:22:25",
      "content": "<p>I don't know what language you're working in, but in python you can read it line by line by using\r\n<br>\r\nopen('file','r').readline() <br>\r\nor <br>\r\nopen('file','r').readlines('Amount of memory, not number of lines')</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "12220",
      "postDate": "07/09/2012 11:40:54",
      "content": "<p>FWIW (<em>i am not very clear on the relationship between files, so this refers just to text processing</em>): the initial and closing curly brackets (in the huge files) are there for no good reason at all, so&nbsp;</p>\r\n<p>1. get rid of those &#43; &quot;textItem&quot;</p>\r\n<p>2. parse each succesive block into a separate line</p>\r\n<p>&nbsp;</p>\r\n<p>I've got a Perl script running now that tries to achieve that, if it works i'll post it here.&nbsp;</p>\r\n<p>&nbsp;</p>\r\n<p>In general, due to the size, the whole thing will be a pain to process...</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "12221",
      "postDate": "07/09/2012 11:46:07",
      "content": "<blockquote>\r\n<p>open('file','r').readline() </p>\r\n</blockquote>\r\n<p>Will read is a text file. That's not a problem, if it were lines of text I could operate on virtually unlimited sized files in a stream. The challenge is parsing it as a JSON object.</p>\r\n<p>Konrad, that's very helpful.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "12222",
      "postDate": "07/09/2012 11:46:07",
      "content": "<blockquote>\r\n<p>open('file','r').readline() </p>\r\n</blockquote>\r\n<p>Will read is a text file. That's not a problem, if it were lines of text I could operate on virtually unlimited sized files in a stream. The challenge is parsing it as a JSON object.</p>\r\n<p>Konrad, that's very helpful.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "12229",
      "postDate": "07/09/2012 16:25:41",
      "content": "<p>Ok, here it is. Since I'm not exactly what you'd define as the world's best Perl programmer, a few words of explanation:</p>\r\n<p>&nbsp;</p>\r\n<ul>\r\n<li>usage is from command line, i.e. perl process2.pl&nbsp;training-non-annotated-text.json\r\n</li><li>I took the list of stopwords to strip from a website, hence the @#% way in which the @stopwords array is defined\r\n</li><li>it's relatively easy to adapt to processing the other large files </li><li>essentially, the idea behind the script is to 1. lump one mention per line 2. remove (English) stopwords at this stage already in order to reduce the file size at the earliest possible stage\r\n</li></ul>\r\n<div>This is very much work in progress, so any comments / improvements / cleaned up versions of the code are welcome.</div>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "12375",
      "postDate": "07/14/2012 01:13:53",
      "content": "<p>Hi,</p>\r\n<p>I think many of you might know this, but since nobody mentioned it, here's a crude way to do it in Python, using the json module in the standard library</p>\r\n<p>&nbsp;</p>\r\n<p>import json</p>\r\n<p>filename = 'training-annotated-text.json'</p>\r\n<p>f = open(filename, 'r')</p>\r\n<p>json_obj = json.load(f)&nbsp;</p>\r\n<p># json_obj is a dict containing a key 'TextItem'</p>\r\n<p>json_data = json_obj['TextItem']</p>\r\n<p># json_data is another dict having the reviews in form of Python lists. These lists are values of the dict.</p>\r\n<p># The lists all contain every word of the review as a string. They have to be concatenated to read the whole review as one string.</p>\r\n<p>&nbsp;</p>\r\n<p>Hope this helps.</p>",
      "rawMarkdown": "",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 12189,
      "author_name": "konradb",
      "author_url": "",
      "post_date": "07/07/2012 06:21:19",
      "content": "<p>I'd start by reading some part of the file as a stream and then dumping into a &quot;sample&quot; file, just to figure out what's going on. While I see &nbsp;your point, I can't believe there's no way around loading the whole thing into memory - I mean, 0.7gb zipped?&nbsp;</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 12190,
      "author_name": "zacstewart",
      "author_url": "",
      "post_date": "07/07/2012 07:02:42",
      "content": "<p>Yeah, I know there has to be a way. But it seems like it would especially be easier if the top-level element was an array rather than an object.</p>\r\n<p>As far as the data structure is concerned, it defines it as a monolithic whole, and splitting out the parts seems like a trick task to me. Not just as easy as pattern matching to delimit array elements.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 12219,
      "author_name": "nmca48984",
      "author_url": "",
      "post_date": "07/09/2012 11:22:25",
      "content": "<p>I don't know what language you're working in, but in python you can read it line by line by using\r\n<br>\r\nopen('file','r').readline() <br>\r\nor <br>\r\nopen('file','r').readlines('Amount of memory, not number of lines')</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 12220,
      "author_name": "konradb",
      "author_url": "",
      "post_date": "07/09/2012 11:40:54",
      "content": "<p>FWIW (<em>i am not very clear on the relationship between files, so this refers just to text processing</em>): the initial and closing curly brackets (in the huge files) are there for no good reason at all, so&nbsp;</p>\r\n<p>1. get rid of those &#43; &quot;textItem&quot;</p>\r\n<p>2. parse each succesive block into a separate line</p>\r\n<p>&nbsp;</p>\r\n<p>I've got a Perl script running now that tries to achieve that, if it works i'll post it here.&nbsp;</p>\r\n<p>&nbsp;</p>\r\n<p>In general, due to the size, the whole thing will be a pain to process...</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 12221,
      "author_name": "zacstewart",
      "author_url": "",
      "post_date": "07/09/2012 11:46:07",
      "content": "<blockquote>\r\n<p>open('file','r').readline() </p>\r\n</blockquote>\r\n<p>Will read is a text file. That's not a problem, if it were lines of text I could operate on virtually unlimited sized files in a stream. The challenge is parsing it as a JSON object.</p>\r\n<p>Konrad, that's very helpful.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 12222,
      "author_name": "zacstewart",
      "author_url": "",
      "post_date": "07/09/2012 11:46:07",
      "content": "<blockquote>\r\n<p>open('file','r').readline() </p>\r\n</blockquote>\r\n<p>Will read is a text file. That's not a problem, if it were lines of text I could operate on virtually unlimited sized files in a stream. The challenge is parsing it as a JSON object.</p>\r\n<p>Konrad, that's very helpful.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 12229,
      "author_name": "konradb",
      "author_url": "",
      "post_date": "07/09/2012 16:25:41",
      "content": "<p>Ok, here it is. Since I'm not exactly what you'd define as the world's best Perl programmer, a few words of explanation:</p>\r\n<p>&nbsp;</p>\r\n<ul>\r\n<li>usage is from command line, i.e. perl process2.pl&nbsp;training-non-annotated-text.json\r\n</li><li>I took the list of stopwords to strip from a website, hence the @#% way in which the @stopwords array is defined\r\n</li><li>it's relatively easy to adapt to processing the other large files </li><li>essentially, the idea behind the script is to 1. lump one mention per line 2. remove (English) stopwords at this stage already in order to reduce the file size at the earliest possible stage\r\n</li></ul>\r\n<div>This is very much work in progress, so any comments / improvements / cleaned up versions of the code are welcome.</div>",
      "votes": null,
      "replies": []
    },
    {
      "id": 12375,
      "author_name": "jaidevd",
      "author_url": "",
      "post_date": "07/14/2012 01:13:53",
      "content": "<p>Hi,</p>\r\n<p>I think many of you might know this, but since nobody mentioned it, here's a crude way to do it in Python, using the json module in the standard library</p>\r\n<p>&nbsp;</p>\r\n<p>import json</p>\r\n<p>filename = 'training-annotated-text.json'</p>\r\n<p>f = open(filename, 'r')</p>\r\n<p>json_obj = json.load(f)&nbsp;</p>\r\n<p># json_obj is a dict containing a key 'TextItem'</p>\r\n<p>json_data = json_obj['TextItem']</p>\r\n<p># json_data is another dict having the reviews in form of Python lists. These lists are values of the dict.</p>\r\n<p># The lists all contain every word of the review as a string. They have to be concatenated to read the whole review as one string.</p>\r\n<p>&nbsp;</p>\r\n<p>Hope this helps.</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "12180": "",
    "12189": "",
    "12190": "",
    "12219": "",
    "12220": "",
    "12221": "",
    "12222": "",
    "12229": "",
    "12375": ""
  },
  "source": "meta"
}