0

I have just retrieved an object using SOAP and HTTPClient from Angular5 and i'm having issues to retrieve specific values. The thing is, I can access only all of them using:

         import {parseString} from 'xml2js';
         ...

         parseString(data, function (err, result) {
           var stringified = JSON.stringify(result);
           console.log(stringified);

          }

For example below is the XML converted to JSON that I have. I have two questions:

1) How would I retrieve the specific << values >> inside of it? Like id, name, event ... InSpec, OutSpec, MiddleSpec and else?

{
    "soap:Envelope": {
        "$": {
            "xmlns:soap": "http://www.w3.org/2003/05/soap-envelope",
            "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
            "xmlns:xsd": "http://www.w3.org/2001/XMLSchema"
        },
        "soap:Body": [{
            "List": [{
                "$": {
                    "xmlns": "http://WebService/"
                },
                "ListResult": [{
                    "Total": ["1"],
                    "Dados": [{
                        "Posicao": [{
                            "id": ["<<value>>"],
                            "name": ["<<value>>"],
                            "event": ["<<value>>"],
                            "address": ["<<value>>"],
                            "description": ["<<value>>"],
                            "addressid": ["<<value>>"],
                            "number": ["<<value>>"],
                            "city": ["<<value>>"],
                            "descriptionEvent": ["<<value>>"],
                            "lat": ["<<value>>"],
                            "long": ["<<value>>"],
                            "In": [{
                                "InSpec": [{
                                    "Descricao": ["<<value>>"],
                                    "Valor": ["false"]
                                }, {
                                    "Descricao": ["<<value>>"],
                                    "Valor": ["false"]
                                }, {
                                    "Descricao": ["<<value>>"],
                                    "Valor": ["false"]
                                }, {
                                    "Descricao": ["<<value>>"],
                                    "Valor": ["false"]
                                }, {
                                    "Descricao": ["<<value>>"],
                                    "Valor": ["true"]
                                }, {
                                    "Descricao": ["<<value>>"],
                                    "Valor": ["false"]
                                }, {
                                    "Descricao": ["<<value>>"],
                                    "Valor": ["false"]
                                }]
                            }],
                            "Out": [{
                                "OutSpec": [{
                                    "Descricao": ["<<value>>"],
                                    "Valor": ["false"]
                                }, {
                                    "Descricao": ["<<value>>"],
                                    "Valor": ["false"]
                                }]
                            }],
                            "Middle": [{
                                "MiddleSpec": [{
                                    "Descricao": ["<<value>>"],
                                    "Valor": ["506"]
                                }, {
                                    "Descricao": ["<<value>>"],
                                    "Valor": ["0"]
                                }, {
                                    "Descricao": ["<<value>>"],
                                    "Valor": ["0"]
                                }, {
                                    "Descricao": ["<<value>>"],
                                    "Valor": ["8"]
                                }, {
                                    "Descricao": ["<<value>>"],
                                    "Valor": ["0"]
                                }, {
                                    "Descricao": ["<<value>>"],
                                    "Valor": ["12.54"]
                                }, {
                                    "Descricao": ["<<value>>"],
                                    "Valor": ["4658"]
                                }, {
                                    "Descricao": ["<<value>>"],
                                    "Valor": ["4.1"]
                                }]
                            }]
                        }]
                    }]
                }]
            }]
        }]
    }
}

2) Is it better to convert from XML to JSON to retrieve these values or is it possible to get them directly from XML?

Appreciate the help.

4
  • If the result is a json object, then you should be able to access it using json['keyAsString'] but i think you shall have to do multiple iterations to get the values you need. What does console.log(result) show? Commented Nov 7, 2017 at 18:29
  • What should I pass to JSON['keyAsString']? Commented Nov 7, 2017 at 19:14
  • what does console.log(result) show?. Should be result['field name as shown in result object'] Commented Nov 7, 2017 at 19:15
  • It returns undefined Commented Nov 7, 2017 at 19:27

1 Answer 1

1

Assuming that you have a stringified json. This works

let jsonObject = JSON.parse(stringified);
console.log('jsonObject', jsonObject);
let jsonEnvelope = jsonObject['soap:Envelope'];
let jsonBody = jsonEnvelope['soap:Body'];
console.log(jsonBody);
let jsonList = jsonBody[0];
console.log(jsonList);
let jsonListResult = jsonList['List'];
console.log(jsonListResult);

Of course a proper answer would be to create a mapper and proper models for your soap response.

Sign up to request clarification or add additional context in comments.

2 Comments

That means I should recreate a variable for each row I would want to jump in? Do you think this is a good way to do this?
As I said a proper solution would require a json mapper. Like here cloudmark.github.io/Json-Mapping. Or you can use an npm package for that, there are some out there. But this one works if you exactly where your data is and is not deep inside the response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.