Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Reading and parsing a JSON object in Ruby

Here's a series of three new examples, building up from one another, showing the reading of a JSON feed from a remote web site in Ruby, and the analysis and presentation of that data.

The first example - [here] - shows the loading of extra modules to handle reading from the web and the JSON format, and the use of the methods loaded fro those modules. Keycode is:

  require 'json'
  require 'open-uri'
  
  source = URI.parse("http://www.wellho.net/services/_.json")
  fh = source.open
  stuff = fh.read
  things = JSON.parse(stuff)


and the example goes on to define and call methods to descend into the resultant object and display it.

The second example - [here] - extends that initial example by providing an insetting class to give a flexibility of display.

And the third example - [here] - implements a second insetting class which outputs the structures loaded from JSON in the form of Ruby source code assignment statements or as a tree depending on a command line option. So the results will look like

  WomanWithCat:kingston grahamellis$ ./tj3.rb -c | head
  timedat = '1433231853'
  lookfor = ''
  acc :
    0 :
      when = '0'
      where = '106.186.17.xx'
      which = '/mouth/1114_PHP-Image-upload-script.html'
    1 :
      when = '0'
      where = '194.187.171.xx'


or

  WomanWithCat:kingston grahamellis$ ./tj3.rb s_name | head
  s_name['timedat'] = '1433231872'
  s_name['lookfor'] = ''
  s_name['acc'] :
  s_name['acc'][0] :
  s_name['acc'][0]['when'] = '1'
  s_name['acc'][0]['where'] = '94.197.121.xx'
  s_name['acc'][0]['which'] = '/pix/crpavatar.jpg'
  s_name['acc'][1] :
  s_name['acc'][1]['when'] = '3'
  s_name['acc'][1]['where'] = '94.197.121.xx'


The source code for these examples is available through the links above, and the code is modestly commented - if you feel that you need to learn more about how it's done, please take a look at our Ruby Training courses, or if you have a group of delegate please enquire about a tailored course at your offices or at our training centre. There are plenty of other examples to help you learn Ruby on our site - see [here] for a complete module list - but there's nothing like having a tutor explain things to you and help you through the initial concepts to get you started!
(written 2015-06-01, updated 2015-06-02)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
R114 - Ruby on the Web
  [4003] Web and console - same principle, same code - Ruby example - (2013-02-14)
  [3773] Ruby on the web - a simple example using CGI - (2012-06-22)
  [3623] Some TestWise examples - helping use Ruby code to check your web site operation - (2012-02-24)
  [3432] 3 digit HTTP status codes - what are they, which are most common, which should be a concern? - (2011-09-11)
  [3431] Ruby at both extremes of your website - (2011-09-10)
  [2607] Answers on Ruby on Rails - (2010-01-30)
  [2605] Ruby on Rails - a sample application to teach you how - (2010-01-30)
  [1891] Ruby to access web services - (2008-11-16)

R105 - Ruby - Classes and Objects
  [4009] Clear, concise examples - Ruby classes and objects. - (2013-02-17)
  [3421] Ruby off the Rails? - (2011-09-06)
  [2651] Calculation within objects - early, last minute, or cached? - (2010-02-26)
  [2616] Defining a static method - Java, Python and Ruby - (2010-02-01)
  [2609] Scope of variables - important to Ruby on Rails - (2010-01-31)
  [2603] Ruby objects - a primer - (2010-01-29)
  [2292] Object Orientation in Ruby - intermediate examples - (2009-07-16)
  [1925] Introduction to Object Oriented Programming - (2008-12-06)
  [983] Blessing in Perl / Member variable in Ruby - (2006-12-14)

R113 - Ruby - Further Input and Output
  [4678] Expect with Ruby - a training example to get you started - (2016-05-18)
  [4676] Running shell (operating system) commands from within Ruby - (2016-05-18)
  [4553] RUby - loading, using, changing, storing JSON format data - (2015-10-23)
  [4008] Reading and checking user inputs - first lessons - Ruby - (2013-02-17)

R110 - Ruby - Special Variables and Pseudo-Variables
  [4682] One line scripts - Awk, Perl and Ruby - (2016-05-20)
  [3757] Ruby - a teaching example showing many of the language features in short but useful program - (2012-06-09)
  [2623] Object Oriented Ruby - new examples - (2010-02-03)
  [2613] Constants in Ruby - (2010-02-01)
  [2296] Variable scope - what is it, and how does it Ruby? - (2009-07-18)
  [1587] Some Ruby programming examples from our course - (2008-03-21)
  [1586] Variable types in Ruby - (2008-03-21)
  [990] Ruby - Totally Topical - (2006-12-16)

R107 - Collections (Arrays and Hashes) in Ruby
  [4499] Significant work - beyond helloworld in Ruby - (2015-05-27)
  [4368] Shuffling a list - Ruby and Python - (2014-12-28)
  [3435] Sorta sorting a hash, and what if an exception is NOT thrown - Ruby - (2011-09-12)
  [3257] All possible combinations from a list (Python) or array (Ruby) - (2011-04-23)
  [3255] Process every member of an array, and sort an array - Ruby - (2011-04-21)
  [3253] Is this number between? Does this list include? - Ruby - (2011-04-18)
  [2976] Creating, extending, traversing and combining Ruby arrays - (2010-09-30)
  [2621] Ruby collections and strings - some new examples - (2010-02-03)
  [2618] What are Ruby Symbols? - (2010-02-02)
  [2606] Sorting arrays and hashes in Ruby - (2010-01-30)
  [2291] Collection objects (array and hash) in Ruby - (2009-07-16)
  [991] Adding a member to a Hash in Ruby - (2006-12-16)


Back to
Defining the behaviour of your web site and testing that it works
Previous and next
or
Horse's mouth home
Forward to
Separating your code for easier testing, understanding and re-use; example in Ruby
Some other Articles
Peak weekend - where there are still rooms near Melksham
Regular Expressions for the petrified - in Ruby
Where does Ruby load modules from, and how to load from current directory
Separating your code for easier testing, understanding and re-use; example in Ruby
Reading and parsing a JSON object in Ruby
Defining the behaviour of your web site and testing that it works
The TransWilts Community Intergrated Transport Corridor
Ruby - where one statement ends and the next begins
Around the world from Melksham
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/4502_Rea ... -Ruby.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat