Using Javascript to parse SOAP responses

Currently, I am working on a Meteor application that requires data consumption from both REST and SOAP APIs. The SOAP service is accessed using the soap package, which functions properly. However, I am facing challenges with the format of the returned data. An example of the data structure is provided below:

{
  "attributes": {
    "xsi:type": "SOAP-ENC:Array",
    "SOAP-ENC:arrayType": "tns:course[1]"
  },
  "item": {
    "attributes": {
      "xsi:type": "tns:course"
    },
    "id": {
      "attributes": {
        "xsi:type": "xsd:int"
      },
      "$value": 1
    },
    "code": {
      "attributes": {
        "xsi:type": "xsd:string"
      },
      "$value": "CDP001"
    },
    // ..... More attributes
    "number_students": {
      "attributes": {
        "xsi:type": "xsd:int"
      },
      "$value": 1
    }
  }
}

My attempt to make the same API call in PHP using its SoapClient resulted in the following data structure (displayed using var_dump):

array(1) {
  [0]=>
  object(stdClass)#2 (8) {
    ["id"]=>
    int(1)
    ["code"]=>
    string(6) "CDP001"
    ["external_course_id"]=>
    NULL
    ["title"]=>
    string(23) "Curso Chamilo de prueba"
    ["language"]=>
    string(7) "spanish"
    ["category_name"]=>
    string(9) "PC Skills"
    ["visibility"]=>
    int(2)
    ["number_students"]=>
    int(1)
  }
}

Unlike PHP's SoapClient, JavaScript seems to struggle with parsing the response into a more "normal" JSON object. Therefore, I am curious if there are any existing JavaScript packages that could handle this task efficiently. While I can create my own solution, it would be beneficial to explore available options.

Your insights will be greatly appreciated,

Answer №1

Most of the time, you can simply disregard the bulk of the information included in your SOAP response.

To efficiently retrieve the necessary data, consider using this concise approach:

const extractedData = {};
for (let key in soap.item) {
    const attributeObj = soap.item[key];
    if ("$value" in attributeObj) {
        extractedData[key] = attributeObj['$value']
    }
}

In essence, if an attribute lacks a $value field, it does not concern us. The resulting object contains:

{
    "id": 1,
    "code": "CDP001",
    "number_students": 1
}

There might be a requirement for customized logic based on the xsi:type, but I believe it is unlikely.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

JavaScript and JSON interchangeably, the first AJAX response should be rewritten by the second response

I am facing an issue with my code: I have two ajax calls being made on window.load, and it seems like the response from the second AJAX call is overwriting the response from the first one before my function can process it. I'm not sure where I'm ...

Cease / Cancel Ajax request without activating an error signal

I am looking for a way to intercept all ajax requests on a page and stop/abort some of the requests based on certain criteria. Although initially using jqXHR.abort(); worked, it caused the error event of all the aborted requests to be triggered, which is n ...

In the following command, where is the PORT stored: ~PORT=8080 npm App.js?

section: Let's consider the following code snippet located in the App.js file: console.log(`This is the port ${process.env.PORT}`); Is there a method to retrieve the value of PORT from outside the running process? ...

Utilizing React to dynamically load JSON data and render a component

I am currently facing a challenge in rendering a React component that includes data fetched from a JSON using the fetch() method. Although the API call is successful, I am experiencing difficulties in displaying the retrieved data. Below is the code snip ...

automatically loading data using jquery

I am currently utilizing a windows service along with an html+j query page to interact with a web service. Whenever a Document is scanned on our device, I aim to display: name, country, and Passport number on our webpage. Although I have successfully a ...

Getting an ETIMEDOUT error while trying to install a package using npm or yarn

In my Arch Linux system, I recently cleared out the npm cache along with other caches. Now, whenever I try to run npm ping or install something using npm or yarn, I encounter the following error: npm notice PING http://registry.npmjs.org/ npm ERR! code ET ...

Are there more efficient methods than having to include require('mongoose') in each models file?

Is it possible to only require mongoose once in the main app.js file and then pass it to other files without loading it again? Will the script do extra work every time the same module is required? var mongoose = require('mongoose'); I'm wo ...

Is it possible for PHP to send an HTTP Get request to Java?

I am currently exploring ways to establish communication between PHP and Java. My goal is to have PHP pass an ID parameter to a Java file, where Java will process the ID and return an array back to PHP. I have researched various methods such as PHP/Java ...

implementing automatic ajax requests when user scrolls

This is a piece of JavaScript code: $(window).scroll(function() { $.ajax({ type: "GET", url: "not_data.php", data: dataString, success: function my_func () { //show new name ...

Tips for utilizing UTC time in defining models with Prisma.js and in general in an Express.js project

Is there a way to Run the node.js project in UTC instead of the local timezone? and How can I specify UTC time when defining a model with prisma.js? model Profile { CreatedDate DateTime @default(now()) // but I need it in UTC ModifiedDate DateTi ...

Why is it that in reactive forms of Angular, the parameter being passed in formControlName is passed as a string?

I am currently working on a reactive form in Angular. In order to synchronize the FormControl object from the TypeScript file with the form control in the HTML file, you need to utilize the formControlName directive. This is accomplished as shown below: f ...

Is Swiper carousel navigation secretly operating without being seen?

I've got a website that utilizes the Swiper carousel from SwiperJS, find it here: An issue I am facing is that the navigation elements are invisible but functional, with the pagination feature unaffected. This peculiar behavior is observed only in Sa ...

Problem with Onsen UI navigation: It is not possible to provide a "ons-page" element to "ons-navigator" when attempting to navigate back to the initial page

Hi, I am having trouble with navigation using Onsen UI. Here is the structure of my app: start.html: This is the first page that appears and it contains a navigator. Clicking on the start button will open page1.html page1.html: Performs an action that op ...

Determine if an Android application has been installed using JavaScript or jQuery

I'm working on an Android app that is also accessible via a web browser. I want to add a banner prompting users to install the Android application if they haven't already. How can I use JavaScript or jQuery to detect if the user has the app insta ...

Is there a way to prevent users from selecting certain days in ion-datetime?

After searching through the official documentation, I couldn't find a solution. I am in need of a function similar to the jQuery datepicker beforeshowday function. My goal is to disable all weekends (Saturday and Sunday) in upcoming dates so that user ...

What impact does manually serializing gRPC request and response objects have on performance?

I am aiming to establish communication with a NodeJS microservice by sending and receiving data. The challenge lies in the fact that both my request and response objects have dynamic components - fields containing a union of 'string' and 'nu ...

Tips for enlarging an image by tapping on it in handlebars

I currently have the handlebars template engine integrated with node.js, and I'm facing an issue where thumbnail images from the database are displaying at a fixed width and height of 70. Is there a way to enable users to click on these images in orde ...

Ways to set up various Content-Security-Policies and headers in your application?

In my email application, I am trying to prevent alerts in JavaScript by using a CSP header. However, even with the current policy in place, alerts can still execute when I send an HTML document attachment that contains script tags. Changing all JavaScript ...

Running code sequentially in a Node.js controller

Recently delved into the world of Node.js, and I'm currently tackling the challenge of running this code synchronously in my controller. Quest.js async function create(req, res, next) { const formValue = req.body['form']; ...

Modify KeyboardDatePicker to display the full name of the day and month

Date Selector Hey there, I'm looking to modify the date format from Wed, Apr 7 to Wednesday, April 7. Is there a way to display the full name of the day and month instead of the short abbreviation? ...