Ruby string/array/json parsing issue

Hey,

I am struggling with this one a bit, the following data is being returned by an API call and I am trying to break it out into the different parts. If we assume that data is stored in variable r, I have been trying code like puts r[‘abuse_type_id’] and it will return a string of abuse_type_id

What I want is the number 1 and for address I would want 1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2

Any ideas? Thanks a lot

{“address”:“1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2”,“count”:2,“first_seen”:“2019-10-17 18:25:15”,“last_seen”:“2020-05-14 01:28:14”,“recent”:[{“abuse_type_id”:1,“abuse_type_other”:“Asking for money”,“description”:“Requesting money”,“created_at”:“2020-05-14T01:28:14.000000Z”},{“abuse_type_id”:2,“abuse_type_other”:null,“description”:“TestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTest”,“created_at”:“2019-10-17T18:25:15.000000Z”}]}

you can use object to parse it.

IRB is your friend:

require 'json'
i=  JSON.parse'{"address":"1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2","count":2,"first_seen":"2019-10-17 18:25:15","last_seen":"2020-05-14 01:28:14","recent":[{"abuse_type_id":1,"abuse_type_other":"Asking for money","description":"Requesting money","created_at":"2020
-05-14T01:28:14.000000Z"},{"abuse_type_id":2,"abuse_type_other":null,"description":"TestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTest","created_at":"2019-10-17T18:25:15.000000Z"}]}'
 => 
{"address"=>"1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2",
... 
3.0.0 :008 > i.keys
 => ["address", "count", "first_seen", "last_seen", "recent"] 
3.0.0 :009 > i.values
 => 
["1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2",
 2,
 "2019-10-17 18:25:15",
 "2020-05-14 01:28:14",
 [{"abuse_type_id"=>1, "abuse_type_other"=>"Asking for money", "description"=>"Requesting money", "created_at"=>"2020-05-14T01:28:14.000000Z"},
  {"abuse_type_id"=>2, "abuse_type_other"=>nil, "description"=>"TestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTest", "created_at"=>"2019-10-17T18:25:15.000000Z"}]] 

hope this helps

Thanks for helping with this!

This post was flagged by the community and is temporarily hidden.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs