Problems Parsing a Json File in Ruby

990 Views Asked by At

I'm pretty new to programming ruby and using json packages, I think I'm facing a typical noob-error, but I just cant find my mistake for two days now.. I've started like this:

require 'discogs-wrapper'
require 'json'

aw = Discogs::Wrapper.new("my_application", user_token: "my_user_token")
inv = aw.get_user_inventory('my_user_name', :per_page => 1, :page => 1)
p=JSON.parse(inv)

What I recieve is this:

C:/Ruby24-x64/lib/ruby/2.4.0/json/common.rb:156:in `initialize': no implicit conversion of Hashie::Mash into String (TypeError)
from C:/Ruby24-x64/lib/ruby/2.4.0/json/common.rb:156:in `new'
from C:/Ruby24-x64/lib/ruby/2.4.0/json/common.rb:156:in `parse'
from C:/Users/rtuz2th/.RubyMine2017.3/config/scratches/scratch.rb:6:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'

I've already been searching your forum and I've learned that this error probably means that I do not have a correct JSON-file (although it comes straight from the discogs-api, I basically thought these guys know what they do). So what I tried next was

require 'discogs-wrapper'
require 'json'

aw = Discogs::Wrapper.new("my_application", user_token: "my_user_token")
inv = aw.get_user_inventory('my_user_name', :per_page => 1, :page => 1)
inv = inv.to_json    
p=JSON.parse(inv)

So at least, my code runs without any errors now. But when I try to refer to any information in inv, I just get empty responses. inv (without the .to_json looks like this:

  {"pagination"=>
  {"per_page"=>1,
   "items"=>13692,
   "page"=>1,
   "urls"=>
    {"last"=>
      "URL containing tokens",
     "next"=>
      "URL containing tokens"},
   "pages"=>13692},
 "listings"=>
  [{"status"=>"Draft",
    "original_price"=>
     {"curr_abbr"=>"EUR",
      "formatted"=>"\u20AC10.46",
      "value"=>10.46,
      "curr_id"=>3},
    "weight"=>230.0,
    "original_shipping_price"=>
     {"curr_abbr"=>"EUR",
      "formatted"=>"\u20AC5.90",
      "value"=>5.9,
      "curr_id"=>3},
    "price"=>{"currency"=>"EUR", "value"=>10.46},
    "allow_offers"=>true,
    "uri"=>"https://www.discogs.com/sell/item/437965910",
    "sleeve_condition"=>"Very Good Plus (VG+)",
    "format_quantity"=>1,
    "id"=>437965910,
    "shipping_price"=>{"currency"=>"EUR", "value"=>5.9},
    "posted"=>"2017-02-07T23:43:01-08:00",
    "ships_from"=>"Germany",
    "in_cart"=>false,
    "comments"=>"",
    "seller"=>
     {"username"=>"zweischeiben.de",
      "stats"=>{"rating"=>"100.0", "total"=>143, "stars"=>5.0},
      "uid"=>3359767,
      "url"=>"https://api.discogs.com/users/zweischeiben.de",
      "html_url"=>"https://www.discogs.com/user/zweischeiben.de",
      "shipping"=>
       "(Lots of information about shipping, cut just cut it out)"
      "payment"=>"Bank Transfer, PayPal",
      "avatar_url"=>
       "(cut out url)",
      "resource_url"=>"https://api.discogs.com/users/zweischeiben.de",
      "id"=>3359767},
    "condition"=>"Near Mint (NM or M-)",
    "release"=>
     {"thumbnail"=>
       "(cut out url)",
      "description"=>"Steamhammer - Mountains (LP, Album, RE)",
      "artist"=>"Steamhammer",
      "format"=>"LP, Album, RE",
      "resource_url"=>"https://api.discogs.com/releases/7303333",
      "title"=>"Mountains",
      "year"=>0,
      "id"=>7303333,
      "catalog_number"=>"201.006, 0201.006"},
                        "resource_url"=>"https://api.discogs.com/marketplace/listings/437965910",
"audio"=>false,
"external_id"=>"3, 16",
"location"=>"T52574"}]}

But if I now try to refer to eg the location:

require 'discogs-wrapper'
require 'json'

aw = Discogs::Wrapper.new("my_application", user_token: "my_token")
inv=aw.get_user_inventory('my_username', :per_page => 1, :page => 1)
inv=inv.to_json
p=JSON.parse(inv)
puts p[location]

I'm just getting an error like this

C:\Ruby24-x64\bin\ruby.exe -e     $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift)             C:/Users/rtuz2th/.RubyMine2017.3/config/scratches/scratch.rb
C:/Users/rtuz2th/.RubyMine2017.3/config/scratches/scratch.rb:9:in `<top (required)>': undefined local variable or method `location' for main:Object (NameError)
from -e:1:in `load'
from -e:1:in `<main>'

Process finished with exit code 1

or an empty response. I'm trying to solve this for two days now, but I'm absolutely out of ideas. Thanks for your help in advance!

1

There are 1 best solutions below

0
On
inv = inv.to_json
p = JSON.parse(inv)

The above is a noop. Use inv as is, it’s a hash already.


puts p[location]

You cannot just lookup whatever key located deeply in the tree hierarchy or you hash. You hash has stings as keys, it has a two top-level elements, 'pagination' and 'listings', the latter is an array, and each element of this array is a hash having location key.


While you were cutting the long value, you dropped the ending comma, making me unable to copy-paste the input and provide the exact answer.