# Xml to ruby compactible json

**URL:** <https://www.ruby-forum.com/t/xml-to-ruby-compactible-json/186476>\
**Category:** Ruby\
**Created:** [April 7, 2010, 5:10pm UTC](https://www.ruby-forum.com/t/xml-to-ruby-compactible-json/186476 "2010-04-07T17:10:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sharnel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@sharnel](https://www.ruby-forum.com/u/sharnel)\
**Post date:** [April 7, 2010, 5:10pm UTC](https://www.ruby-forum.com/t/xml-to-ruby-compactible-json/186476/1 "2010-04-07T17:10:52Z")

</div>

Hi,

I have an xml which i need in ruby json format

I use the below code :

data=Hash.from\_xml(xml).to\_json()

where xml contains the xml data.  
i get the result in the below format :

{‘responseHeader’:{‘status’:0,‘QTime’:0}}

but i require the result as :  
{  
‘responseHeader’=\>{  
‘status’=\>0,  
‘QTime’=\>0}  
}

Kindly help

---

<div class="post-metadata">

**Author:** ![sharnel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@sharnel](https://www.ruby-forum.com/u/sharnel)\
**Post date:** [April 7, 2010, 5:37pm UTC](https://www.ruby-forum.com/t/xml-to-ruby-compactible-json/186476/2 "2010-04-07T17:37:54Z")

</div>

Smp Mp wrote:

> I use the below code :
> 
> data=Hash.from\_xml(xml).to\_json()
> 
> where xml contains the xml data.  
> i get the result in the below format :
> 
> {‘responseHeader’:{‘status’:0,‘QTime’:0}}

That looks like valid JSON to me.

> but i require the result as :  
> {  
> ‘responseHeader’=\>{  
> ‘status’=\>0,  
> ‘QTime’=\>0}  
> }

That’s not valid JSON. You mean you want a Ruby Hash? Try

data = Hash.from\_xml(xml)

(there’s no point converting it into JSON if you’re going to JSON.parse  
it back into a hash)

---

<div class="post-metadata">

**Author:** ![sharnel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecc23a/32.png) [@sharnel](https://www.ruby-forum.com/u/sharnel)\
**Post date:** [April 8, 2010, 12:35am UTC](https://www.ruby-forum.com/t/xml-to-ruby-compactible-json/186476/3 "2010-04-08T00:35:45Z")

</div>

Thanks.
