Last active
March 22, 2022 16:41
-
-
Save matt-west/c8bd63418a2ba02f4056 to your computer and use it in GitHub Desktop.
Ruby script to split a JSON file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'json' | |
puts "Opening File" | |
file = File.open('input.json') | |
puts "Fetching Contents" | |
content = file.read | |
puts "Parsing JSON" | |
json = JSON.parse(content) | |
puts "Producing Files" | |
segment_size = 100.0 | |
totalItems = json.length | |
loops = (totalItems / segment_size).ceil | |
puts "Total Objects: " + json.length.to_s | |
puts "Total Loops: " + loops.to_s | |
# Create Files | |
loops.times do |i| | |
puts "Creating file #{i + 1}" | |
segment = json.slice(i * segment_size, segment_size) | |
# Write places to the places.json file | |
File.open("output.#{i + 1}.json", 'w') do |segment_file| | |
segment_file.write segment.to_json | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment