Vijikumar M
Vijikumar M

Reputation: 3764

need to get data from a string using Ruby

I have a string as mentioned below.

str="[{\"id\":\"100\",\"sal_id\":10009,\"playback_url\":\"http:www.example_com\",\"profile\":\"v5\",\"protocol\":\"ttp\",\"vwidth\":40,\"vheight\":30,\"vbitrate\":1200,\"abitrate\":160,\"status\":\"queue\"}]"

from the above string i want to take only the playback_url value, i.e http:www.example.com.

I have tried with split but its not working. like

str.split("playback_url").join(',')

Please help me to achieve that. Thanks in advance.

Upvotes: 0

Views: 114

Answers (1)

LHH
LHH

Reputation: 3323

Try this

require 'json'

str="[{\"id\":\"100\",\"sal_id\":10009,\"playback_url\":\"http:www.example_com\",\"profile\":\"v5\",\"protocol\":\"ttp\",\"vwidth\":40,\"vheight\":30,\"vbitrate\":1200,\"abitrate\":160,\"status\":\"queue\"}]"

result = JSON.parse(str)

[{"id"=>"100", "sal_id"=>10009, "playback_url"=>"http:www.example_com", "profile"=>"v5", "protocol"=>"ttp", "vwidth"=>40, "vheight"=>30, "vbitrate"=>1200, "abitrate"=>160, "status"=>"queue"}] 

result.first["playback_url"]
 => "http:www.example_com" 

Upvotes: 5

Related Questions