Created
September 6, 2016 10:04
-
-
Save dineshprabu-freshdesk/75b5a5201967fbe52c3518d5ee33233c to your computer and use it in GitHub Desktop.
Zendesk JSON to XML
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'active_support/all' | |
def json_to_xml root_key, file_name | |
file_name = "#{file_name}.json" unless file_name.ends_with? ".json" | |
xml_doc = '<?xml version="1.0" encoding="UTF-8"?>'+"<#{root_key}s>" | |
(File.readlines file_name).each do |line| | |
hash_to_xml = (JSON.parse(line).to_xml) | |
xml_doc = xml_doc + (((hash_to_xml.gsub "<hash>", "<#{root_key}>").gsub "</hash>", "</#{root_key}>").gsub('<?xml version="1.0" encoding="UTF-8"?>', '')) | |
end | |
xml_doc = xml_doc+"</#{root_key}s>" | |
File.write "#{root_key}.xml", xml_doc | |
end | |
json_to_xml "organization", "organization.json" | |
json_to_xml "user", "users.json" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment