fred
fred

Reputation: 151

how to read individual data from the content of remote JSON file in Ruby on Rails?

I am trying to read individual data from the content of json API on Oil and Gas Authority website; however the code I have returns all the data. Any help would be highly appreciated.

require 'json'
require 'open-uri'

def index
  url='http://data-ogauthority.opendata.arcgis.com/datasets/ab4f6b9519794522aa6ffa6c31617bf8_0.geojson'
  @result = JSON.parse open(url).read
end 

This my index view:

<% @result.each do |row| %>
  <%= row %> 
<% end %> 

Upvotes: 3

Views: 631

Answers (3)

Gerry
Gerry

Reputation: 10507

Also to add to @jvillian answer, you can fetch the filtered information using this link; for example, taking into account the fields you need:

  • FIELDNAME
  • FIELDTYPE
  • STATUS
  • DISC_DATE
  • DISC_WELL

You could create query that will result in the following response:

{
 "displayFieldName": "FIELDNAME",
 "fieldAliases": {
  "FIELDNAME": "Field Name",
  "FIELDTYPE": "Field Type",
  "STATUS": "Status",
  "DISC_DATE": "Discovery Date",
  "DISC_WELL": "Discovery Well"
 },
 "fields": [
  {
   "name": "FIELDNAME",
   "type": "esriFieldTypeString",
   "alias": "Field Name",
   "length": 32
  },
  {
   "name": "FIELDTYPE",
   "type": "esriFieldTypeString",
   "alias": "Field Type",
   "length": 4
  },
  {
   "name": "STATUS",
   "type": "esriFieldTypeString",
   "alias": "Status",
   "length": 50
  },
  {
   "name": "DISC_DATE",
   "type": "esriFieldTypeString",
   "alias": "Discovery Date",
   "length": 20
  },
  {
   "name": "DISC_WELL",
   "type": "esriFieldTypeString",
   "alias": "Discovery Well",
   "length": 20
  }
 ],
 "features": [
  {
   "attributes": {
    "FIELDNAME": "GRYPHON",
    "FIELDTYPE": "OIL",
    "STATUS": "PRODUCING",
    "DISC_DATE": "1987/07",
    "DISC_WELL": "9/18b-7"
   }
  },
  {
   "attributes": {
    "FIELDNAME": "BRAEMAR",
    "FIELDTYPE": "COND",
    "STATUS": "PRODUCING",
    "DISC_DATE": "1995/05",
    "DISC_WELL": "16/03b-8Z"
   }
  },
  ...
 ]
}

This file will be now 76KB, and you can extract the data in almost the same way, just change properties for attributes, i.e.:

<% @result[:features].each do |feature| %>
  <%= feature[:attributes][:FIELDNAME] %>
  <%= feature[:attributes][:FIELDTYPE] %>
  ...
<% end %>

Upvotes: 0

dimitry_n
dimitry_n

Reputation: 3019

One thing to add to @jvillian answer is that if one of the keys is nil then calling this branch's subsequent keys will result in undefined method '[]'. Ruby 2.3+ has a new method called dig which will simply return nil. More on dig in my answer to this question.

Upvotes: 1

jvillian
jvillian

Reputation: 20253

Given that the API (as you are currently using it) returns a JSON structure like this:

{
  "type":"FeatureCollection",
  "features":[
    {
      "type":"Feature",
      "properties":{
        "FIELDNAME":"GRYPHON",
        "FIELDTYPE":"OIL",
        "NAME_SHORT":"GRYPHON",
        "STATUS":"PRODUCING",
        "DISC_DATE":"1987/07",
        "DISC_WELL":"9/18b-7",
        "STAT_CODE":"800",
        "PROD_DATE":"1993/10",
        "DEPTH_M":"111.86",
        "DET_STATUS":"DETERMINED",
        "ORIG_OP":"KERR-MCGEE",
        "ORIG_LIC":"P.496",
        "ORIG_LIC_2":"P.478",
        "ORIG_LIC_3":"P.257",
        "ORIG_LIC_4":"P.103",
        "ORIG_LIC_5":" ",
        "CURR_OPER":"MAERSK OIL NORTH SEA UK LIMITED",
        "FIELDDATA":"https://itportal.decc.gov.uk/fields/fields_index/ukcs+field+information+listed+by+field+name/183.htm",
        "OBJECTID":16,
        "OGA_COP":null
      },
      "geometry":{
        "type":"Polygon",
        "coordinates":[
          [
            [1.5701447246411744,59.35253688325039],
            ...
          ]
        ]
      }
    },
    ...
  ]
}

You could do something like:

<% @result[:features].each do |feature| %>
  <%= feature[:properties][:FIELDNAME] %>
  <%= feature[:properties][:FIELDTYPE] %>
  ...
<% end %>

Your JSON file looks to be something like 1.3MB. So, unless you can figure out how to filter your results on the API side (using query params, I would suppose), you may end up with various performance issues in retrieving the JSON.

And, you may want to do:

@result = JSON.parse(open(url).read).with_indifferent_access

So that you can use symbols to access hash elements as well as strings.

Upvotes: 3

Related Questions