RUby - loading, using, changing, storing JSON format data
Archive - Originally posted on "The Horse's Mouth" - 2015-10-23 14:02:23 - Graham Ellisjson is an excellent data exchange format - it "serialises" a data structure that consists of arrays and hashes (or whatever ordered and unordered collections are called in the language you're using) so that the structure can be saved, transferred between programs and computers, and reloaded easily.
All of the languages we teach (I think) has classes that support JSON and it's very much the modern 'standard'. And you can grab some json from our website at http://www.wellho.net/service/_.json to test out our sample programs.
Today's sample - as I've been running a Ruby course is in Ruby.
Starting with reading and parsing from a file:
require 'json'
fh = File.new 'sample.json'
content = fh.read
info = JSON.parse content
... yes, really that simple. content contains the raw (json string) data, and info contains the fully loaded Hash and List structure. Full program [here] goes on to loook at
• displaying elements of the data
• changing the data
• saving the modified structure into a new file of Json.