Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Perl Converting XML to JSON and JSON back to XML

I'm trying to convert some some XML response data to JSON retrieved through an API call for the front-end to use. As my Web Application client and server communicates using JSON, however I have needed to make use of a third party API which uses XML for response and message HTTP API communication. Can't seem to find a Perl XML to JSON module that yields i deal output of the XML to JSON conversion below. I've used XML::XML2JSON and XML::Hash then converted it to JSON but both that adds $t/$text for text attributes, result: "Status": {$t: "OK"}, my application expects "Status": "OK" as the format of JSON.

Sample XML:

<Response>
  <Status>OK</Status> 
  <Job>
    <ID>J000309</ID> 
    <Name>job name</Name> 
    <Description>description of job</Description> 
    <State>Planned</State> 
    <ClientOrderNumber />
    <Type>Website Development</Type> 
    <StartDate>2008-08-29T00:00:00</StartDate> 
    <DueDate>2008-09-29T00:00:00</DueDate> 
    <InternalID>234234234</InternalID>
    <CompletedDate>2008-09-29T00:00:00</CompletedDate> 
    <Client>
      <ID>307</ID> 
      <Name>The Health Company</Name> 
    </Client>
    <Contact>
      <ID>267</ID> 
      <Name>Shelley Brio</Name> 
    </Contact>
  </Job>
</Response>

Perl XML2JSOn module output.

{
  "Response": {
    "Status": {"$t": "OK"},
    "Job": {
      "ID": {"$t": "J000309"},
      "Name": {"$t": "job name}",
      "Contact": {
        "ID": {"$t": "267"},
        "Name": {"$t": "Shelley Brio"}
      }
    }
  }
}

The ideal output is:

{
  "Response": {
    "Status": "OK",
    "Job": {
      "ID": "J000309",
      "Name": "job name",
      "Description": "description of job",
      "State": "Planned",
      "Type": "Website Development",
      "StartDate": "2008-08-29T00:00:00",
      "DueDate": "2008-09-29T00:00:00",
      "InternalID": "234234234",
      "CompletedDate": "2008-09-29T00:00:00",
      "Client": {
        "ID": "307",
        "Name": "The Health Company"
      },
      "Contact": {
        "ID": "267",
        "Name": "Shelley Brio"
      }
    }
  }
}

Because the 'ideal output' is the format the JSON is passed back to the server (through a POST request) the JSON to XML conversion fails as its formatted incorrect.

Comments