Skip to content

Instantly share code, notes, and snippets.

@matt-west
Last active March 22, 2022 16:41
Show Gist options
  • Save matt-west/c8bd63418a2ba02f4056 to your computer and use it in GitHub Desktop.
Save matt-west/c8bd63418a2ba02f4056 to your computer and use it in GitHub Desktop.
Ruby script to split a JSON file
#!/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