2

I have a text file (below) that is produced from a script that can not be altered. I am getting the file from the server and then I want to turn this "string" of json into real json using a ruby script...

Example of a file produced:

{"Key": "value", "Key2": "value2", "Key3": "value3"}
{"Key": "value", "Key2": "value2", "Key3": "value3"}
{"Key": "value", "Key2": "value2", "Key3": "value3"}

Note the lack of proper json formatting and lack of ANY commas... Thanks in advance for your help!

2
  • Is it strictly one object per line? Commented Jan 30, 2016 at 3:24
  • Yes, it's always only one object per line. Commented Jan 30, 2016 at 3:39

2 Answers 2

1

Assuming the objects are one entry per line, you can do the following:

require 'json'

objects = $stdin.each_line.map { |line| JSON.parse(line) }
puts JSON.pretty_generate(objects)

Then just run ruby clean.rb < log.txt > output.json. For compact JSON, use JSON.dump instead of JSON.pretty_generate.

If you want to save a little time and memory by avoiding parsing and generating JSON, you can do the following:

prev = nil
print '['
$stdin.each_line do |line|
  print prev.strip + "," if prev
  prev = line
end
print prev.strip + ']'

A similar approach would be to use sed to append commas to each line and surround everything with brackets.

Sign up to request clarification or add additional context in comments.

1 Comment

Prefer each_line to read.lines to avoid reading the entire file into memory at once.
1

You can also use eval to convert the string into a ruby hash. Here is a simple Ruby solution that does not require I/O redirection:

 require 'json'

 array_of_hashes = []
 File.open('hash.txt').each_line do |line|
   array_of_hashes << eval(line)
 end


 puts array_of_hashes.to_json

Returns:

 [{"Key":"value","Key2":"value2","Key3":"value3"},{"Key":"value","Key2":"value2","Key3":"value3"},{"Key":"value","Key2":"value2","Key3":"value3"}]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.