steve_gallagher
steve_gallagher

Reputation: 3898

Format a quoted array string into an array

I have data like:

"[{\"workstationName\":\"Test Workstation Id 123\"},{\"workstationName\":\"Alex's Workstation\"}]"

I want it to simply be:

[{"workstationName":"Test Workstation Id 123"},{"workstationName":"Alex's Workstation"}]

I should know this. I tried a.to_ary, but no good. Any straight-forward way to process this? Thanks.

Upvotes: 0

Views: 77

Answers (2)

New Alexandria
New Alexandria

Reputation: 7324

JSON.parse is what you're looking for

Upvotes: 1

Arup Rakshit
Arup Rakshit

Reputation: 118271

How is this ?

require 'json'
require 'yaml'

JSON.parse("[{\"workstationName\":\"Test Workstation Id 123\"},{\"workstationName\":\"Alex's Workstation\"}]")
# => [{"workstationName"=>"Test Workstation Id 123"},
#     {"workstationName"=>"Alex's Workstation"}]

YAML.load("[{\"workstationName\":\"Test Workstation Id 123\"},{\"workstationName\":\"Alex's Workstation\"}]")
# => [{"workstationName"=>"Test Workstation Id 123"},
#     {"workstationName"=>"Alex's Workstation"}]

Upvotes: 3

Related Questions