J. Davidson
J. Davidson

Reputation: 3307

Parsing result for nested value from api response

Hi I get a payload from soap api as result. from result I did following

console.log(util.inspect(result, false, null));

I get following result

{ 'S:Envelope': 
             { '$' : { 'xmlns:s : 'http://.....'},
             'S:Body':
               { 'n:sendresponse':
                 { '$': {'xmlns:ns2': 'http://......'},
                   tranResult:{id: '', temp: '224'}}}}

What I want to do is parse value of temp. I am doing something like this

var resl=util.inspect(result, false, null);

I dont know how to proceed with it. Please let me know how I can get value of temp from the result and assign it to var temperature Thanks

Upvotes: 1

Views: 152

Answers (2)

Behrooz
Behrooz

Reputation: 2371

JSON is a first class citizen in JavaScript and you can traverse its structure easily like a tree. In the given example, you want to traverse further down until you get to temp. You can go from the root node (the object itself) to the child element by calling the child element name either directly using dot notation (e.g., parent.child) or using brackets if it has special characters or if the child name is stored in another variable (e.g., parent['child']).

For the given example, the path to temp is o['S:Envelope']['S:Body']['n:sendresponse']['tranResult']['temp'].

Upvotes: 1

Hearen
Hearen

Reputation: 7838

If it's a json object, you first have to parse it to get an object which will make it easier to access the member temp and retrieve its value.

How to work with object in JS. Check working with object.

Parsing json, you can use either JSON.parse(jsonString) or jsonString.toJSON() (if you can in your case) to get the object.

Upvotes: 0

Related Questions