Ruby - Parse txt file to json -


how should parse txt file below format read json in ruby?

thank you.

{"sales": "52,000,000", "id": 12, "comp_name": "friedfood"} {"sales": "51,000,000", "id": 1, "comp_name": "copley"} {"sales": "54,000,000", "id": 2, "comp_name": "tony's"} {"sales": "52,000,789", "id": 3, "comp_name": "j&j"} 

parse each line json object, this:

require 'json' items = file.open('tmp.json', 'r').each_line.map { |l| json.parse(l) } puts items.to_s 

edit: modified use map, based on comment below.


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -