例えば http://hoge.com/data.xml というXMLを返すAPIがあったとして、ここからJSONデータに変換したいなら以下のコード。 ただしActiveSupportが必要なので、Rails以外の環境では、ActiveSupportを別途インストールすること。 require 'active_support/core_ext' require 'open-uri' hash = Hash.from_xml open('http://hoge.com/ext/data.xml').read json = hash.to_json
![XMLをJSONに変換する - Qiita](https://cdn-ak-scissors.b.st-hatena.com/image/square/6469a330280bc8934c220ca30be0b8e8dbebfcea/height=288;version=1;width=512/https%3A%2F%2Fqiita-user-contents.imgix.net%2Fhttps%253A%252F%252Fcdn.qiita.com%252Fassets%252Fpublic%252Farticle-ogp-background-412672c5f0600ab9a64263b751f1bc81.png%3Fixlib%3Drb-4.0.0%26w%3D1200%26mark64%3DaHR0cHM6Ly9xaWl0YS11c2VyLWNvbnRlbnRzLmltZ2l4Lm5ldC9-dGV4dD9peGxpYj1yYi00LjAuMCZ3PTk3MiZoPTM3OCZ0eHQ9WE1MJUUzJTgyJTkySlNPTiVFMyU4MSVBQiVFNSVBNCU4OSVFNiU4RiU5QiVFMyU4MSU5OSVFMyU4MiU4QiZ0eHQtYWxpZ249bGVmdCUyQ3RvcCZ0eHQtY29sb3I9JTIzMUUyMTIxJnR4dC1mb250PUhpcmFnaW5vJTIwU2FucyUyMFc2JnR4dC1zaXplPTU2JnM9ZTYxMTlkM2JlYTg1OWUyZjU4NzczMmY1MmExNGU5NGE%26mark-x%3D142%26mark-y%3D57%26blend64%3DaHR0cHM6Ly9xaWl0YS11c2VyLWNvbnRlbnRzLmltZ2l4Lm5ldC9-dGV4dD9peGxpYj1yYi00LjAuMCZoPTc2Jnc9NzcwJnR4dD0lNDB5X21hdHN1d2l0dGVyJnR4dC1jb2xvcj0lMjMxRTIxMjEmdHh0LWZvbnQ9SGlyYWdpbm8lMjBTYW5zJTIwVzYmdHh0LXNpemU9MzYmdHh0LWFsaWduPWxlZnQlMkN0b3Amcz02MTQyMTNkZjkyYzI2M2UxNDk1NGIwMjU0ODljM2U3Yg%26blend-x%3D142%26blend-y%3D436%26blend-mode%3Dnormal%26txt64%3DaW4gRE1NLmNvbQ%26txt-width%3D770%26txt-clip%3Dend%252Cellipsis%26txt-color%3D%25231E2121%26txt-font%3DHiragino%2520Sans%2520W6%26txt-size%3D36%26txt-x%3D156%26txt-y%3D536%26s%3D95691b33df2c3a1386f4f55735ab78ab)