Skip to content

Instantly share code, notes, and snippets.

@dineshprabu-freshdesk
Created September 6, 2016 10:04
Show Gist options
  • Save dineshprabu-freshdesk/75b5a5201967fbe52c3518d5ee33233c to your computer and use it in GitHub Desktop.
Save dineshprabu-freshdesk/75b5a5201967fbe52c3518d5ee33233c to your computer and use it in GitHub Desktop.
Zendesk JSON to XML
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