Reputation: 4828
I'm developing a Meteor application that has to consume data from both a REST and SOAP APIs. For accessing the SOAP service I'm using the soap package, that works fine but I'm having trouble with the format of the returned data. Here's an example:
{
"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
}
}
}
I've tried the same API call using PHP and its SoapClient
and this is the returned data (printed 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)
}
}
As you can see, PHP's SoapClient takes care of parsing the response to provide an easily accessible object. My question is, do you know any Javascript package that could parse the response and return a more "normal" JSON object? I can build it myself, but perhaps there's already a solution out there.
Many thanks,
Upvotes: 0
Views: 629
Reputation: 8053
For the most part, you can ignore the majority of what's returned in your SOAP response.
A quick method like this will extract the data you need:
const obj = {};
for (let key in soap.item) {
const attributeObj = soap.item[key];
if ("$value" in attributeObj) {
obj[key] = attributeObj['$value']
}
}
Basically, if the attribute doesn't have a $value
field, we're not interested in it. The final object:
{
"id": 1,
"code": "CDP001",
"number_students": 1
}
You may need to do some special logic depending on the xsi:type
, if it stores it weirdly, but I don't think so
Upvotes: 1