Newbie question here. I am calling a making a web service call in node.js using node-soap and getting a response similar to the following:
{ AuthenticateResult:
{ PrimaryKeyId: '0',
ValidateOnly: false,
OperationResult: 'Succeeded',
SessionId: 'abc45235435345' } }
What is the best way to extract the SessionId value from the response if OperationResult is 'Succeeded'? I am guessing I could do it with indexof and substring, but even to newbie like me, this doesn't sound like a nice solution.
Assuming the input is stored as a string, and is consistent (i.e. JSON without the quotes to the left of the colon), you can just use a regex to convert it into a JSON string, then use JSON.parse()
.
var input = "{ AuthenticateResult: {\n"
+ "PrimaryKeyId: '0',\n"
+ "ValidateOnly: false,\n"
+ "OperationResult: 'Succeeded',\n"
+ "SessionId: 'abc45235435345' } }";
// This replaces word: with "word": and ' with "
var json = input.replace(/(\w+):/g, '"$1":').replace(/'/g, '"');
// This here's the object you want
var obj = JSON.parse(json);
// Just printing out the JSON so you know it works.
document.getElementById('result').innerHTML = json;
<pre id="result"></pre>