convert request parameter of array hashes to ruby hashes -


i have following request parameters:

"mappings"=>"[{ \"spec_id\" => \"1\",  \"item_name\" => \"sku\"}|{ \"spec_id\" => \"2\",  \"item_name\" => \" productname\"}|{ \"spec_id\" => \"4\",  \"item_name\" => \" price\"}]" 

i'd know how can parse items in hashes.

the first thing

mappings = params[:mapping][:mappings].split("|")  mappings.each |map|    # don't know how create hashes end 

i prefer split on "," instead of "|" if possible , i'm not 100% sure if request parameter in correct format. if isn't please let me know , change it.

to parse string, following:

str = "[{ \"spec_id\" => \"1\",  \"item_name\" => \"sku\"}|{ \"spec_id\" => \"2\",  \"item_name\" => \" productname\"}|{ \"spec_id\" => \"4\",  \"item_name\" => \" price\"}]"  mappings = json.parse(str.gsub(/}\s*\|\s*{/, '},{').gsub(/\s*\=\>/, ':')) 

this convert have json string removing '|' characters , converting '=>' ':'. when parse result you'll parsing json, you'll nice ruby hash:

[{"spec_id"=>"1", "item_name"=>"sku"}, {"spec_id"=>"2", "item_name"=>" productname"}, {"spec_id"=>"4", "item_name"=>" price"}] 

Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -