I've got a bit of a headache here - I'm pretty new to Ruby...
I've got a structure like so returned from Savon...
{
:item => [{
:key => "result",
:value=> "success"
},
{
:key => "data",
:value=> {
:item => [{ :key => "displayName",
:value => "Matt"
},
{
:key => "messages",
:value => {
:items => [{....}]
}
]
}
}]
}
And I need to get it to be like...
{
:result => success
:data => [{
:displayName => "Matt"
},
{
:messages => [{
:messageName => "Test Message"
}]
}]
}
So I can easily manipulate and navigate the data structure I've tried a few methods including just passing the value of :item ...
def convertData(data)
result = {}
if(data!=nil)
data.each do |item|
key = item[:key]
value = item[:value]
if(value.instance_of?(Hash))
result[key] = convertData(value[:item])
else
result[key] = value
end
end
end
return result
end
But this just gives me a tonne of Type errors for symbol to integer conversion (I assume that's the array index playing up). Any help much appreciated.
When trying to tackle problem like this in Ruby, it's useful to think of it in terms of transformations on your data. The Ruby Enumerable library is full of methods that help you manipulate regular data structures like Array and Hash.
A Ruby solution to this problem looks like:
Here's some sample input data and sample output:
I think this version looks better, output-wise, but that's a call you'll have to make:
Note that the
case
statement here is really the key, it allows you to quickly differentiate between different types of data you're converting, and theHash[]
method converts key-value pairs into the Hash structure you're looking for.This is similar to your attempt, but just passes through content it doesn't recognize as-is.