JSON string to rails hash - ruby-on-rails

I'm on Rails 2.3 and I'm trying to convert a string that is JSON-formatted to a Rails hash. However, when I use JSON.parse I get a JSON string without the delimiters:
{"source_id":40007,"object":"86088947610496.1","coursewalk_id":"86088947610477.1","description":"","image_uri":"db\/db-files\/
Image_2011-09-24_14.37.37__0000.jpg","latitude":"38.0113439821061","letter":"","letter_A":"","letter_B":"","letter_C":"","lett
er_D":"","letter_E":"","letter_F":"","longitude":"-78.7576854509104","number":"1","mcw_id":71}
Results of JSON.parse:
number1letter_Bcoursewalk_id86088947610477.1letter_Clatitude38.0113439821061letter_Dletter_Eletter_Fmcw_id71longitude-78.75768
54509104letterdescriptionobject86088947610496.1source_id40007letter_Aimage_uridb/db-files/Image_2011-09-24_14.37.37__0000.jpg
Code:
puts string_to_parse
fence_parsed = JSON.parse(string_to_parse)
puts fence_parsed
Any ideas?
Thanks,
Nick

That's just because you're using "puts". If you just type fence_parsed, or p fence_parsed you'll get what you're looking for. puts calls to_s. I hope this clears it up for you, if not let me know and I'll elaborate.

Related

Rails params to array

I am sending a list of checkbox selected from PHP file to our Rails API server. All checked items' ID's will be sent in json format (campaign_ids in json_encode from PHP):
I got a URL being passed to our API like this
Started PUT "/campaigns/function.json?campaign_ids=["6","7"]&user_id=0090000007"
I need to get the campaign_ids ["6","7"] and process it like any other array using array.each do || end
How can I convert this to an array so I can use array.each?
The following sample code can achieve it but I think there could be a better way?
campaign_ids = params[:campaign_ids].to_s # [\"6\",\"7\"]
campaign_ids = campaign_ids.gsub(/[^0-9,]/,'') # 6,7
if campaign_ids.size.to_i > 0 # 3 ??
campaign_ids.split(",").each do |campaign_id|
...
end
end
The correct format of the URL should've been campaign_ids[]=6&campaign_ids[]=7. That would automatically yield an array of [6, 7] when you do params[:campaign_ids].
But assuming you can't change the format of the incorrect parameters, you can still get it via JSON.parse(params[:campaign_ids])
Try this
campaign_ids = JSON.parse(params[:campaign_ids])
You get params[:campaign_ids] as a string.
So, you will have to parse that json string to get array elements.
params[:campaign_ids] is already in your desired array format, you need not convert that to string using to_s.
You can do something like this
campaign_ids = params[:campaign_ids]
campaign_ids.each do |campaign_id|
# do the computation here
end

JSON::ParserError: 757: unexpected token at '{

the current hash is
{\"report_name\"=>\"Study/Control: ABIRATERONE ACETATE - 20151413355\", \"left_mue_start_date\"=>\"02-26-2015\", \"left_mue_end_date\"=>\"03-19-2015\", \"right_mue_start_date\"=>\"02-26-2015\", \"right_mue_end_date\"=>\"03-19-2015\", \"report_formulary_id\"=>\",7581\", \"mue\"=>\"true\", \"mue_type\"=>\"study/control\", \"chain_id\"=>\"1\", \"left_mue_formulary_ids\"=>[\"7581\"], \"action\"=>\"create_report\", \"controller\"=>\"informatics\", \"user_id\"=>339}
now I need to convert it in proper hash like
{"report_name" => "Study/Control: ABIRATERONE ACETATE - 20151413355"}
so I am trying to get it with JSON.parse but I am getting error like:
JSON::ParserError: 757: unexpected token at '{
So if someone know about that so please help me.
and I am using Rails 3.2
What you have is a hash printed as String. To convert it into a Hash use eval.
ch = "{\"report_name\"=>\"Study/Control: ABIRATERONE ACETATE - 20151413355\", \"left_mue_start_date\"=>\"02-26-2015\", \"left_mue_end_date\"=>\"03-19-2015\", \"right_mue_start_date\"=>\"02-26-2015\", \"right_mue_end_date\"=>\"03-19-2015\", \"report_formulary_id\"=>\",7581\", \"mue\"=>\"true\", \"mue_type\"=>\"study/control\", \"chain_id\"=>\"1\", \"left_mue_formulary_ids\"=>[\"7581\"], \"action\"=>\"create_report\", \"controller\"=>\"informatics\", \"user_id\"=>339}"
hash = eval(ch)
# => {"report_name"=>"Study/Control: ABIRATERONE ACETATE - 20151413355", "left_mue_start_date"=>"02-26-2015", "left_mue_end_date"=>"03-19-2015", "right_mue_start_date"=>"02-26-2015", "right_mue_end_date"=>"03-19-2015", "report_formulary_id"=>",7581", "mue"=>"true", "mue_type"=>"study/control", "chain_id"=>"1", "left_mue_formulary_ids"=>["7581"], "action"=>"create_report", "controller"=>"informatics", "user_id"=>339}
PS: A JSON string should look as follows, meaning what you have is not JSON and hence you got JSON::ParserError for using JSON.parse on a non-JSON string :
"{\"report_name\":\"Study/Control: ABIRATERONE ACETATE - 20151413355\",\"left_mue_start_date\":\"02-26-2015\",\"left_mue_end_date\":\"03-19-2015\",\"right_mue_start_date\":\"02-26-2015\",\"right_mue_end_date\":\"03-19-2015\",\"report_formulary_id\":\",7581\",\"mue\":\"true\",\"mue_type\":\"study/control\",\"chain_id\":\"1\",\"left_mue_formulary_ids\":[\"7581\"],\"action\":\"create_report\",\"controller\":\"informatics\",\"user_id\":339}"
To avoid using eval you could use JSON.parse ch.gsub('=>', ':') this way you will get a HASH from your HASH stored as STRING
Last time when I got this issue, since the json file that I got from a API that contains a BOM
UTF-8 BOM is a sequence of bytes (EF BB BF)
What's different between UTF-8 and UTF-8 without BOM?
at the beginning of that string, but you know that part wouldn't display or as readable when we got the string from response. I try to use Ruby JSON to parse it, but I failed, I got the same exception with yours. just a reminder for others, when you get a Json response. By the way, this would be no problem while you are handling that in javascript, but with problems in Python or Ruby languages.
I ran into a similar problem, though it was failing while parsing \"\". This is in regards to using Pact.IO. I mention it here since this is the highest ranked Google result while looking for the error I encountered. The solution in my case was to change the body of a POST in my C# application so that it wasn't using empty string, but a null string. Basically I added this before my HTTP call.
if (request.Method == HttpMethod.Post && request.Content!=null && request.Content.GetType()==typeof(StringContent) && request.Content.ReadAsStringAsync().Result == String.Empty)
request.Content = new StringContent(null);

rails decode json cyrrilic string

I have such string example, which i get from json (cp1251):
Ôèëüòð ìàñëÿíûé OPEL/GM/DAEWOO
which mean:
Фильтр масляный OPEL/GM/DAEWOO
this tool http://www.artlebedev.ru/tools/decoder/ say that i must use CP1252 → CP1251 decoder. I try it so:
my_string.force_encoding('cp1252').force_encoding('1251')
but it didn't solve my problem. What i do wrong?
how could i convert to normall view my json cyrrillic string in RoR?
i get json from url so:
jsonAE = JSON.load(open('http://******/portal.api?l=*****&p=Sih2*****&act=price_by_nr_firm&nr='+article_nr+'&oe=true'))
from json i get:
{"result":[{"nr":"OC90","brand":"Knecht","name":"Фильтр масляный OPEL/GM/DAEWOO","stock":"-","delivery":"не известно","minq":"1","upd":"16.03.15 23:40","price":"130.34","currency":"руб."},{"nr":"OC90","brand":"Knecht","name":‌​"Фильтр масляный OPEL/GM/DAEWOO","stock":"-","delivery":"не известно","minq":"1","upd":"17.03.15 00:05","price":"130.34","currency":"руб."}]}
but it turn's to something bad with JSON.load
▶ puts 'Ôèëüòð ìàñëÿíûé OPEL/GM/DAEWOO'
.encode(Encoding::CP1252)
.force_encoding(Encoding::CP1251)
.encode(Encoding::UTF_8)
#⇒ Фильтр масляный OPEL/GM/DAEWOO
The string in ruby is suspected to be utf8ed. So, the first action is to inform ruby that the string is actually in one-byte. Than we say “hey, don’t care, I know this one-byte is actually cyrillic.” And, finally, turn it back to utf-8.
Hope it helps.

Regex extract substring ruby

"{\"status\":1,\"redirect\":\"/some/uri/uri2/index.html?post_login=80607979823520\",\"security_token\":\"/cpsess8233434446\"}"
I am getting this response as string and I need to extract security_token value.
I tried to convert the string to Hash by eval method.seems not worked and I need to do a regex match.
You can do this:
require 'json'
a = JSON.load "{\"status\":1,\"redirect\":\"/some/uri/uri2/index.html?post_login=80607979823520\",\"security_token\":\"/cpsess8233434446\"}"
p a["security_token"] #=> "/cpsess8233434446"
You need to parse the JSON data..
result = "{\"status\":1,\"redirect\":\"/some/uri/uri2/index.html?post_login=80607979823520\",\"security_token\":\"/cpsess8233434446\"}"
h = JSON.parse(result)
h['security_token'] # => "/cpsess8233434446"
You can either JSON.load the data and filter for ['security_token'] or use a .match(/security_token/) style regex expression.
I'd suggest the prior for future readability and code maintenance.

Mapping to the Keys of a Hash

I am working with a hash called my_hash :
{"2011-02-01 00:00:00+00"=>816, "2011-01-01 00:00:00+00"=>58, "2011-03-01 00:00:00+00"=>241}
First, I try to parse all the keys, in my_hash (which are times).
my_hash.keys.sort.each do |key|
parsed_keys << Date.parse(key).to_s
end
Which gives me this :
["2011-01-01", "2011-02-01", "2011-03-01"]
Then, I try to map parsed_keys back to the keys of my_hash :
Hash[my_hash.map {|k,v| [parsed_keys[k], v]}]
But that returns the following error :
TypeError: can't convert String into Integer
How can I map parsed_keys back to the keys of my_hash ?
My aim is to get rid of the "00:00:00+00" at end of all the keys.
Why don't you just do this?
my_hash.map{|k,v| {k.gsub(" 00:00:00+00","") => v}}.reduce(:merge)
This gives you
{"2011-02-01"=>816, "2011-01-01"=>58, "2011-03-01"=>241}
There is a new "Rails way" methods for this task :)
http://api.rubyonrails.org/classes/Hash.html#method-i-transform_keys
Using iblue answer, you could use a regexp to handle this situation, for example:
pattern = /00:00:00(\+00)+/
my_hash.map{|k,v| {k.gsub(pattern,"") => v}}.reduce(:merge)
You could improve the pattern to handle different situations.
Hope it helps.
Edit:
Sorry, iblue have already posted the answer
Another alternative could be:
map
return converted two elements array [converted_key, converted_value]
convert back to a hash
irb(main):001:0> {a: 1, b: 2}.map{|k,v| [k.to_s, v.to_s]}.to_h
=> {"a"=>"1", "b"=>"2"}

Resources