Let's suppose that we have the following JSON Object that describes a Person:
{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address":
{
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
},
"phoneNumber":
[
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "fax",
"number": "646 555-4567"
}
]
}
Can someone suggest the most elegant and most efficient way to manipulate the previous object in Rails 3?
I want to be able to:
Thanks in advance.
PS. I prefer to manipulate it in the Controller!
just parse it and change it
hash = JSON.parse(json_data)
hash["firstname"] = "John"
hash.delete("lastname")
new_json = hash.to_json
P.S. JSON.parse might not work - it depends on which JSON library you're using. try this instead:
ActiveSupport::JSON.decode(json_data)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With