0

I have JSON object result which is converted from xml data response as mentioned below.

{
    "env:Envelope": {
        "xmlns:env": [
            "http://schemas.xmlsoap.org/soap/envelope/"
        ],
        "xmlns:wsa": [
            "http://www.w3.org/2005/08/addressing"
        ],
        "env:Header": [
            {
                "wsa:MessageID": [
                    "urn:E8CC4000D1BF11EA8F1C35236A977E2C"
                ],
                "wsa:FaultTo": [
                    {
                        "wsa:Address": [
                            "http://www.w3.org/2005/08/addressing/anonymous"
                        ],
                        "wsa:ReferenceParameters": [
                            {
                                "instra:tracking.compositeInstanceCreatedTime": [
                                    {
                                        "_": "2020-07-29T12:21:16.054-05:00",
                                        "xmlns:instra": [
                                            "http://xmlns.o.com/sca/tracking/1.0"
                                        ]
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        ],
        "env:Body": [
            {
                "processResponse": [
                    {
                        "xmlns": [
                            "http://xmlns.o.com/BPL_AutoRFC"
                        ],
                        "payload": [
                            {
                                "xmlns:dvm": [
                                    "http://www.o.com/XSL/Transform/java/tip.dvm.LookupValue"
                                ],
                                "xmlns": [
                                    ""
                                ],
                                "ns0:RfcProject": [
                                    {
                                        "xmlns:ns0": [
                                            "http://www.siebel.com/xml/RFC"
                                        ],
                                        "ns0:IntegrationStatus": [
                                            "RFC Query - Success"
                                        ],
                                        "ns0:Id": [
                                            "3-APXME9Y
                                        ],
                ]
            }
        ]
    }
}

The code am using below:

(async () => {
  const { response } = await soapRequest({ url: url, headers: sampleHeaders, xml: xml, timeout: 10000 }); // Optional timeout parameter(milliseconds)
  const { headers, body, statusCode } = response;
  console.log(headers);
  xml2js.parseString(body,{ mergeAttrs: true }, (err, result) => {
    if(err) {
        throw err;
    }

    // `result` is a JavaScript object
    // convert it to a JSON string
    const json = JSON.stringify(result, null, 4);

    // log JSON string
    console.log(json);
    
});

})();

I need to fetch below value from the above JSON object result. Is there anyway i can fetch the value.

Below value i need to fetch from JSON object.Also i many cases i need to fetch the value like this.

"ns0:Id": 3-APXME9Y

Can any one help in this

1
  • JSON is a text representation of some data structure. You don't need to use JSON here (maybe only to ensure that console.log() prints everything). result is a JavaScript object, all you have to do is to access its properties using the square bracket notation. Commented Jul 29, 2020 at 19:20

1 Answer 1

1

Your JSON is not valid (missing " and some ] } ) as far as I can tell.

For the rest I am not sure what you are exactly trying to do but you can access the properties of the JSON object. Don't stringify, just start with result

let v=result["env:Envelope"]["env:Body"][0].processResponse[0].payload[0]["ns0:RfcProject"][0]["ns0:Id"][0]

This should assign the string "3-APXME9Y" in v

However keep in mind that this approach to is somehow flawed. ns0 actually refers to the namespace "http://www.siebel.com/xml/RFC" and could possibly change. If you are dealing with XML, just deal with XML and don't convert to JSON, as it is not namespace aware.

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

2 Comments

In xml response how to retrieve the data like this , can you please help me
First you need to retrieve an XML document. Im not familiar with soapRequest but would advise you to start from your constant body. Post the actual XML instead of the JSON. Then for XML traversal the best way is to use XPath (developer.mozilla.org/en-US/docs/Web/XPath)/…)

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.