Handling multiple JSON objects in a single file

I did something stupid ... I collected a ton of json data and saved it in a single file. Now I get errors when trying JSON.parse (file) due to its JSON object after the JSON object. Can anyone advise how I can parse this data? The line is as follows. The next entry is an object with the same structure.

{"purchase": 
  { 
   "amount": 34.595399, 
   "uid": 1282907706, 
   "user": 
          {
                 "id": xxxx
                 "name": "xxxx"
          }, 
    "dailycount": 135.82373100000001, 
    "productdetails": 
     {
          "type": "shoes"
     }, 
    "details": 
    {
          "gender": "male"
   }, 
   "createdin": "Asia/Tokyo", 
   "id": 147707740, 
   "comments": []}} 
+3
source share
2 answers

The problem, of course, is that you do not have a valid json object.

irb(main):004:0> JSON.parse("{'foo':'bar'},{'foo':'baz'}")
JSON::ParserError: 705: unexpected token at '{'foo':'bar'},{'foo':'baz'}'
    from /usr/lib/ruby/gems/1.8/gems/json-1.4.6/lib/json/common.rb:146:in `parse'
    from /usr/lib/ruby/gems/1.8/gems/json-1.4.6/lib/json/common.rb:146:in `parse'
    from (irb):4

So, I would add a “key”: ['and'] 'to the end of your last curly brace. to make it valid json.

irb(main):018:0> json = '{"key" : [{"foo":"bar"}, {"foo":"baz"}]}'
=> "{\"key\" : [{\"foo\":\"bar\"}, {\"foo\":\"baz\"}]}"
irb(main):019:0> JSON.parse json
=> {"key"=>[{"foo"=>"bar"}, {"foo"=>"baz"}]}
+4
source

? , :

{
   "a" : "b",
   "c" : "d"
}
{
   "a" : "e",
   "c" : "f",
}

, }\s*{ }, { :

[
{
   "a" : "b",
   "c" : "d"
},
{
   "a" : "e",
   "c" : "f",
}
]
+3

Source: https://habr.com/ru/post/1761909/


All Articles