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
Post a Comment