Convert TSV files to JSON format and JSON to TSV files
Ruby 2.6.5 Mac OS 10.15.5
[{"name":"john","gender":"m","age":"18"},
{"name":"paul","gender":"m","age":"20"},
{"name":"alice","gender":"f","age":"15"},
{"name":"dabid","gender":"m","age":"17"},
{"name":"jasmin","gender":"f","age":"17"}]
Name gender age
john m 18
paul m 20
alice f 15
dabid m 17
jasmin f 17
require "json" # Described to handle JSON
File.open("meibo.json") do |json| #Open JSON file
File.open("meibo.txt", "w") do |txt| #Open the output TSV file
array = JSON.load (json) #Load JSON
column = ["name", "gender", "age"] #header
txt.puts (column.join ("\ t")) # Insert header
array.each do |line| #Put each element of the JSON array on each line of the TSV file
attr = [line["name"],line["gender"],line["age"]]
txt.puts(attr.join("\t"))
end
end
end
First, open the JSON file. Also open the output destination TSV file in write mode.
Next, read the JSON file. As shown below, JSON reads a JSON file with JSON.load ([File object])
and returns it as an array with a hash as an element.
require "json"
File.open("meibo.json") do |json|
array = JSON.load(json)
p array
end
=>
[{"name"=>"john", "gender"=>"m", "age"=>"18"}, {"name"=>"paul", "gender"=>"m", "age"=>"20"}, {"name"=>"alice", "gender"=>"f", "age"=>"15"}, {"name"=>"dabid", "gender"=>"m", "age"=>"17"}, {"name"=>"jasmin", "gender"=>"f", "age"=>"17"}]
It is an image that the returned array is turned by the ʻeach method and each hash is made line by line in TSV. As a technique to put in each line of TSV, the value of each hash fetched by ʻeach
is fetched by directly specifying the key, and they are put back in the array once. Then, use the join
method to join the arrays by tab delimiter and insert the string into each line of the TSV file.
The output result is as follows.
name gender age
john m 18
paul m 20
alice f 15
dabid m 17
jasmin f 17
At first, as shown below, when the array after JSON.load
was sent to ʻeach` processing, it was stored in TSV in the order of the values of each object.
** The order of the JSON members (key and value set) does not matter **.
That is, {" name ":" john "," gender ":" m "," age ":" 18 "}
and {" name ":" john "," age ":" 18 "," gender " : "m"}
is indistinguishable.
Therefore, this time it happens that there is no problem if you convert to TSV in the order of the JSON members, but if the JSON is in a different order, it will be strange when converted to TSV. It was.
So, it's troublesome, but once you take out the value by specifying the hash key, put it in the array in the order of the columns, and then put it in the TSV, it is sure.
require "json"
File.open("meibo.json") do |json|
File.open("meibo.txt", "w") do |txt|
array = JSON.load(json)
column = ["name","gender","age"]
txt.puts(column.join("\t"))
array.each do |line|
txt.puts (line.values.join ("\ t")) Enter into TSV according to the order of values of each object of #JSON
end
end
end
require "json"
require "csv"
hash_ary = []
File.open("meibo.json","w") do |json|
CSV.foreach("meibo.txt",col_sep: "\t", headers: true) do |line|
h = {name: line[0], gender: line[1], age: line[2]}
hash_ary << h
end
Use JSON.dump (hash_ary, json) #to_json
end
First, prepare a file for writing JSON in write mode.
The TSV file is a CSV class that uses the foreach
method to convert each line into a hash. Put the converted hash in the array (here, hash_ary
), and after all the lines have been inserted, convert the hash to JSON with JSON.dump
and write it to the file.
The output result is as follows.
[{"name":"john","gender":"m","age":"18"},{"name":"paul","gender":"m","age":"20"},{"name":"alice","gender":"f","age":"15"},{"name":"dabid","gender":"m","age":"17"},{"name":"jasmin","gender":"f","age":"17"}]
require "json"
require "csv"
hash_ary = []
File.open("meibo.json","w") do |json|
CSV.foreach("meibo.txt",col_sep: "\t", headers: true) do |line|
h = {name: line[0], gender: line[1], age: line[2]}
hash_ary << h.to_json
end
json << hash_ary
end
At first, it was written as above, but if you do it like this. ..
["{\"name\":\"john\",\"gender\":\"m\",\"age\":\"18\"}", "{\"name\":\"paul\",\"gender\":\"m\",\"age\":\"20\"}", "{\"name\":\"alice\",\"gender\":\"f\",\"age\":\"15\"}", "{\"name\":\"dabid\",\"gender\":\"m\",\"age\":\"17\"}", "{\"name\":\"jasmin\",\"gender\":\"f\",\"age\":\"17\"}"]
As you can see, \
was inserted before "
and it could not be converted successfully.
The to_json
method returns a JSON-formatted ** string **, so using JSON.dump
instead of this solved the problem.
Recommended Posts