jsonrubyrest-clientdashing

Extract Single Data From JSON URL for Dashing Dashboard


I am trying to show the "sgv" value on a Dashing / Smashing dashboard widget. Ultimately I would also like to show the "direction" value as well. I am running into problems pulling that precise value down which changes every 3 to 5 minutes. I have already been able to mirror the exact string using the following:

require 'net/http'
require 'rest-client'
require 'json'

url = "https://dnarnianbg.herokuapp.com/api/v1/entries/current.json"
response = RestClient.get(url)
JSON.parse(response)

# :first_in sets how long it takes before the job is first run. In this case, it is run immediately
current_nightscout = 0

SCHEDULER.every '5m' do 
  last_nightscout = current_nightscout
  current_nightscout = response

  send_event('nightscout', { current: current_nightscout, last: last_nightscout })
 end

I have also searched the archives several times. I don't wish to write this to a file like this one shows and the duplicate question has been deleted or moved.

I realize that the JSON.parse(response) is just going to parse out whatever I tell it the response equals, but I don't know how to get that response to equal SGV. Maybe the solution isn't in the RestClient, but that is where I am lost.

Here is the JSON URL: http://dnarnianbg.herokuapp.com/api/v1/entries/current.json

EDIT: The output of that link is something like this:

[{"_id":"5ba295ddb8a1ee0aede71822","sgv":87,"date":1537381813000,"dateString":"2018-09-19T18:30:13.000Z","trend":4,"direction":"Flat","device":"share2","type":"sgv"}]


Solution

  • You need something like response[0]["sgv"] which should return 52 if you end up with many items in the list you will need to iterate over them.

    The best thing you can do is to break your problem down into easier parts to debug. As you are having problems accessing some JSON via an API you should make a simple script which only does the function you want in order to test it and see where the problem is.

    Here is a short example you can put into a .rb file and run;

    #!/usr/bin/ruby
    
    require 'open-uri'
    require 'json'
    
    test = JSON.parse(open("https://dnarnianbg.herokuapp.com/api/v1/entries/current.json", :read_timeout => 4).read)
    
    puts test[0]["sgv"]
    

    That should return the value from sgv

    I realise that short sweet example may be little use as a learner so here is a more verbose version with some comments;

    #!/usr/bin/ruby
    
    require 'open-uri'
    require 'json'
    
    # Open the URL and read the result. Time out if this takes longer then 4 sec.
    get_data = open("https://dnarnianbg.herokuapp.com/api/v1/entries/current.json", :read_timeout => 4).read
    
    # Parse the response (get_data) to JSON and put in variable output
    output = JSON.parse(get_data)
    
    # Put the output to get the 'sgv figure'
    p output[0]["sgv"]
    

    It always pays to manually examine the data you get back, in your case the data looks like this (when make pretty)

    [
      {
        "_id": "5ba41a0fb8a1ee0aedf6eb2c",
        "sgv": 144,
        "date": 1537481109000,
        "dateString": "2018-09-20T22:05:09.000Z",
        "trend": 4,
        "direction": "Flat",
        "device": "share2",
        "type": "sgv"
      }
    ]
    

    What you actually have is an Array. Your server returns only 1 result, numbered '0' hence you need [0] in your p statement. Once you have accessed the array id then you can simply use the object you need as [sgv]

    If your app ever returns more than one record then you will need to change your code to read all of the results and iterate over them in order to get all the values you need.