How can I read individual data from the content of a remote JSON file in Ruby on Rails?

I am trying to read individual data from the json API content on the Oil and Gas Authority website; however the code I have is returning all data. Any help would be much 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 is my index pointer:

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

      

+3


source to share


3 answers


Considering the API (how you use 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 can do something like:

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

      



Your JSON file looks like 1.3MB. So if you can't figure out how to filter the results on the API side (given query parameters, I would guess), you might run into various performance issues when fetching the JSON.

And you might want to do:

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

      

So that you can use symbols

to access items hash

as well as strings

.

+3


source


One thing to add to @ jvillian's answer is that if one of the keys nil

, then calling those subsequent subsequent branches will result in undefined method '[]'

. Ruby 2.3+ has a new method dig

that will just return nil

. More on dig

in the answer to this question.



+1


source


Also, to add to @jvillian's answer , you can get the filtered information using this link ; for example, given the fields you need:

  • FIELDNAME
  • FieldType
  • STATUS
  • DISC_DATE
  • DISC_WELL

You can create a query that will lead to the following answer:

{
 "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 now be 76KB and you can retrieve data in much the same way, just change properties

to attributes

, that is:

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

      

0


source







All Articles