Skip to content

Unmarshalled object to JSON? #168

Closed
Closed

Description

@sytolk

It is written that this library can turn XML into JSON. I'm trying to write Node.js proxy that turn SOAP service into Rest (media JSON). And I have successful unmarshal XML response from SOAP service. The problem is that I cannot send JSON model from const unmarshall. unmarshall Object is:

nodeName: 'ns2:GetFuelStationsResponse',
  tagName: 'ns2:GetFuelStationsResponse',
  namespaceURI: 'http://schemas.mobility-media.com/wsdl/fuelprice/v1',
  prefix: 'ns2',
  localName: 'GetFuelStationsResponse',
  parentNode:  ...

How about to have method unmarshall.toJson() to extract values from this object?
This is my code:

const context = new Jsonix.Context([envelope, getFuelStationResponse]);
const unmarshaller = context.createUnmarshaller();

// Unmarshal the XML file
const unmarshall = unmarshaller.unmarshalString(xml);
//to get JSON string is not possible from  unmarshall
console.log(unmarshall.value.body.any[0].childNodes[0].childNodes[1].childNodes); 

Activity

  1. highsource commented on May 9, 2017

    @highsource
    Owner

    Something is wrong with your mappings, you seem to get DOM object, probably your any property is mapped as DOM element and does not have a corresponding type.

    Normally, if everything's mapped you should be getting pure JSON. Please PR your test project under https://github.com/highsource/jsonix-support/f/fuelprice, I'll see about it.

  2. highsource commented on May 9, 2017

    @highsource
    Owner

    Here's the XML you try to unmarshall:

    <?xml version="1.0" encoding="UTF-8"?>
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
       <SOAP-ENV:Header xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" />
       <soap:Body>
          <ns2:GetFuelStationsResponse xmlns:ns2="http://schemas.mobility-media.com/wsdl/fuelprice/v1" xmlns:ns3="http://schemas.mobility-media.com/wsdl/fuelprice/v1/model" xmlns:ns4="http://schemas.mobility-media.com/wsdl/fuelprice/v1/exception">
             <ns2:entry>
                <ns3:fuelStation id="50014" lastUpdate="2017-05-09T19:00:13.965Z" provider="mdm">
                   <ns3:location>
                      <ns3:latitude>52.6543</ns3:latitude>
                      <ns3:longitude>8.2416</ns3:longitude>
                   </ns3:location>
                   <ns3:address>
                      <ns3:line1>Steinfelder Str. 6</ns3:line1>
                      <ns3:city>Lohne</ns3:city>
                      <ns3:postalCode>49393</ns3:postalCode>
                      <ns3:country>Germany</ns3:country>
                   </ns3:address>
                   <ns3:name>Tankshop TS Lohne GmbH</ns3:name>
                   <ns3:brand />
                   <ns3:openingHours>
                      <ns3:default>
                         <ns3:item day="0">
                            <ns3:interval>
                               <ns3:start hours="5" minutes="0" />
                               <ns3:end hours="23" minutes="0" />
                               <ns3:orderNum>0</ns3:orderNum>
                            </ns3:interval>
                         </ns3:item>
                         <ns3:item day="1">
                            <ns3:interval>
                               <ns3:start hours="5" minutes="0" />
                               <ns3:end hours="23" minutes="0" />
                               <ns3:orderNum>0</ns3:orderNum>
                            </ns3:interval>
                         </ns3:item>
                         <ns3:item day="2">
                            <ns3:interval>
                               <ns3:start hours="5" minutes="0" />
                               <ns3:end hours="23" minutes="0" />
                               <ns3:orderNum>0</ns3:orderNum>
                            </ns3:interval>
                         </ns3:item>
                         <ns3:item day="3">
                            <ns3:interval>
                               <ns3:start hours="5" minutes="0" />
                               <ns3:end hours="23" minutes="0" />
                               <ns3:orderNum>0</ns3:orderNum>
                            </ns3:interval>
                         </ns3:item>
                         <ns3:item day="4">
                            <ns3:interval>
                               <ns3:start hours="5" minutes="0" />
                               <ns3:end hours="23" minutes="0" />
                               <ns3:orderNum>0</ns3:orderNum>
                            </ns3:interval>
                         </ns3:item>
                         <ns3:item day="5">
                            <ns3:interval>
                               <ns3:start hours="7" minutes="0" />
                               <ns3:end hours="23" minutes="0" />
                               <ns3:orderNum>0</ns3:orderNum>
                            </ns3:interval>
                         </ns3:item>
                         <ns3:item day="6">
                            <ns3:interval>
                               <ns3:start hours="8" minutes="0" />
                               <ns3:end hours="23" minutes="0" />
                               <ns3:orderNum>0</ns3:orderNum>
                            </ns3:interval>
                         </ns3:item>
                         <ns3:item day="7">
                            <ns3:interval>
                               <ns3:start hours="8" minutes="0" />
                               <ns3:end hours="23" minutes="0" />
                               <ns3:orderNum>0</ns3:orderNum>
                            </ns3:interval>
                         </ns3:item>
                      </ns3:default>
                   </ns3:openingHours>
                   <ns3:country code="DE" timezone="Europe/Berlin" />
                   <ns3:providerId>110DF6CB-1471-434A-E928-041D3D293EF5</ns3:providerId>
                </ns3:fuelStation>
                <ns3:prices stationId="50014">
                   <ns3:price fuelType="ft_e10" provider="mdm" stationId="50014">
                      <ns3:price currency="EUR">1.379</ns3:price>
                      <ns3:validFrom>2017-04-07T05:07:55.613Z</ns3:validFrom>
                      <ns3:lastUpdate>2017-04-07T05:07:55.613Z</ns3:lastUpdate>
                   </ns3:price>
                   <ns3:price fuelType="ft_e5" provider="mdm" stationId="50014">
                      <ns3:price currency="EUR">1.399</ns3:price>
                      <ns3:validFrom>2017-04-07T05:07:55.636Z</ns3:validFrom>
                      <ns3:lastUpdate>2017-04-07T05:07:55.636Z</ns3:lastUpdate>
                   </ns3:price>
                   <ns3:price fuelType="ft_diesel" provider="mdm" stationId="50014">
                      <ns3:price currency="EUR">1.189</ns3:price>
                      <ns3:validFrom>2017-04-07T05:07:55.590Z</ns3:validFrom>
                      <ns3:lastUpdate>2017-04-07T05:07:55.590Z</ns3:lastUpdate>
                   </ns3:price>
                </ns3:prices>
             </ns2:entry>
          </ns2:GetFuelStationsResponse>
       </soap:Body>
    </soap:Envelope>
    

    The problem is that your schemas/mappings do not contain mappings for the GetFuelStationsResponse element. So Jsonix does not know how to unmarshal this element and therefore leaves DOM.

    Please augment your schemas/mappings with top level element for GetFuelStationsResponse and corresponding types. You should have a global element like
    <xs:element name="getFuelStationsResponse" type="GetFuelStationsResponseType"/> or similar somewhere.

  3. sytolk commented on May 10, 2017

    @sytolk
    Author

    Yes you are right this is mapping problem. I have fix it and push changes here:
    https://github.com/sytolk/jsonix-support/tree/master/f/fuelprice.
    Maybe it's need to have exception/warning in log for the mapping problem before Jsonix to leaves DOM?
    I don`t know if this is OK but after body element from schema -> envelope.xsd its have any[0].getFuelStationsResponse.xsd This is the code that display parsed JSON now:

    console.log(JSON.stringify(unmarshalled.value.body.any[0].value));
    

    t.e. it's have two value for the two XSL schemas separated with any[0] or it's have some problem with body element mapping too. but this is not a big problem..
    Can I skip TYPE_NAME JSON property? I think it is useless I only want to return JSON string in response (like Rest service do this) and use the same Mapping objects in Rest client app.

    "fuelStation":{"TYPE_NAME":"getFuelStationsResponse.FuelStation","id":50014, ...
    
  4. sytolk commented on May 11, 2017

    @sytolk
    Author

    It`s fixed now thanks @highsource

  5. vasyland commented on Dec 1, 2017

    @vasyland

    sytolk
    Could you please share an example on your fix? Thanks.

  6. sytolk commented on Dec 2, 2017

    @sytolk
    Author

    @vasyland I have change library to strong-soap + ws for SOAP (XML) to JSON converter

    const soap = require('strong-soap').soap;
    const ws = require('ws.js');
    const XMLHandler = soap.XMLHandler;
    const xmlHandler = new XMLHandler();
     const ctx = {
                request: request
                , url: url
                , action: "urn:" + endpoint
                , contentType: "text/xml"
            };
    
            const handlers = [new ws.Http()];
    
            ws.send(handlers, ctx, function (ctx) {
                const root = xmlHandler.xmlToJson(null, ctx.response, null); 
            });
    
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions