Tom Hammond
Tom Hammond

Reputation: 6080

Rails Convert XML to json

I'm attempting to convert XML to JSON, but I'm losing data in doing so.

In the XML I most care about two things:

<value code="1" cint:variable-id="11734028" cint:global-variable-id="2740" cint:country-language-variable-id="36688">American Indian or Alaska Native<text xml:lang="x-native">American Indian or Alaska Native                                                                                                                                                                                                                                                                            </text></value>
  1. The cint:variable-id="11734028"
  2. The actual text value - American Indian or Alaska Native

When I convert the XML to JSON using Hash.from_xml(xml).to_json it seems to lose the information within the value tag.

The XML is this:

xml = '<?xml version="1.0" encoding="utf-8" standalone="no"?>
<surveys xmlns:cint="http://www.cint.com/">
  <sss version="2.0" languages="x-native" xmlns:cint="http://www.cint.com/">
    <date>2017-06-28</date>
    <time>17:00:35</time>
    <survey>
      <title>Ethnicity<text xml:lang="x-native">Ethnicity</text></title>
      <record ident="c" cint:category-id="63608">
        <variable ident="451539" type="single" cint:sort-order="1" cint:hide-on="Registration" cint:country-language-question-id="2222">
          <name>Ethnicity - US &amp; Canada only</name>
          <label>How would you define yourself as it relates to your ethnicity? (US and Canada only)<text xml:lang="x-native">How would you define yourself as it relates to your ethnicity?</text></label>
          <position start="1" />
          <values>
            <value code="1" cint:variable-id="11734028" cint:global-variable-id="2740" cint:country-language-variable-id="36688">American Indian or Alaska Native<text xml:lang="x-native">American Indian or Alaska Native                                                                                                                                                                                                                                                                            </text></value>
            <value code="2" cint:variable-id="11734029" cint:global-variable-id="2741" cint:country-language-variable-id="36689">Asian<text xml:lang="x-native">Asian                                                                                                                                                                                                                                                                                                       </text></value>
            <value code="3" cint:variable-id="11734030" cint:global-variable-id="2742" cint:country-language-variable-id="36690">Black or African American<text xml:lang="x-native">Black or African American                                                                                                                                                                                                                                                                                   </text></value>
            <value code="4" cint:variable-id="11734031" cint:global-variable-id="2743" cint:country-language-variable-id="36691">Hispanic or Latino<text xml:lang="x-native">Hispanic or Latino                                                                                                                                                                                                                                                                                          </text></value>
            <value code="5" cint:variable-id="11734032" cint:global-variable-id="2744" cint:country-language-variable-id="36692">Native Hawaiian or Pacific Islander<text xml:lang="x-native">Native Hawaiian or Pacific Islander                                                                                                                                                                                                                                                                         </text></value>
            <value code="6" cint:variable-id="11734033" cint:global-variable-id="2745" cint:country-language-variable-id="36693">White<text xml:lang="x-native">White                                                                                                                                                                                                                                                                                                       </text></value>
            <value code="98" cint:variable-id="11734034" cint:global-variable-id="2746" cint:country-language-variable-id="36694">Other<text xml:lang="x-native">Other                                                                                                                                                                                                                                                                                                       </text></value>
            <value code="99" cint:variable-id="11734035" cint:global-variable-id="2747" cint:country-language-variable-id="36695">I prefer not to answer<text xml:lang="x-native">Prefer not to say                                                                                                                                                                                                                                                                                           </text></value>
          </values>
        </variable>
      </record>
    </survey>
  </sss>
</surveys>'

The output JSON is this:

"{\"surveys\":{\"xmlns:cint\":\"http://www.cint.com/\",\"sss\":{\"version\":\"2.0\",\"languages\":\"x-native\",\"xmlns:cint\":\"http://www.cint.com/\",\"date\":\"2017-06-28\",\"time\":\"17:00:35\",\"survey\":{\"title\":\"Ethnicity\",\"record\":{\"ident\":\"c\",\"cint:category_id\":\"63608\",\"variable\":{\"ident\":\"451539\",\"type\":\"single\",\"cint:sort_order\":\"1\",\"cint:hide_on\":\"Registration\",\"cint:country_language_question_id\":\"2222\",\"name\":\"Ethnicity - US \\u0026 Canada only\",\"label\":\"How would you define yourself as it relates to your ethnicity? (US and Canada only)\",\"position\":{\"start\":\"1\"},\"values\":{\"value\":[\"American Indian or Alaska Native\",\"Asian\",\"Black or African American\",\"Hispanic or Latino\",\"Native Hawaiian or Pacific Islander\",\"White\",\"Other\",\"I prefer not to answer\"]}}}}}}}"

Any ideas how I can convert this to json without losing the information within the value tag?

Upvotes: 0

Views: 1522

Answers (1)

Brian
Brian

Reputation: 5481

There's a note in the documentation regarding an inability of Hash.from_xml to properly parse attributes.

You will probably have better luck using a true XML parser such as Nokogiri.

Upvotes: 1

Related Questions