-2

I can not get the value from an http response body.

I've used JSON.parse() on the response.body I get as well as xml-js library. The value I have to get is 'P01'. This is the response:

{  
   "Soap:Envelope":{  
      "_attributes":{  
         "xmlns:Soap":"http://schemas.xmlsoap.org/soap/envelope/"
      },
      "Soap:Body":{  
         "ValidateUser_Result":{  
            "_attributes":{  
               "xmlns":"urn:microsoft-dynamics-schemas/codeunit/UserValidation"
            },
            "return_value":{  
               "_text":"P01"
            }
         }
      }
    }
}

and here is what I've tried:

console.log(JSON.parse(data["Soap:Envelope"]["Soap:Body"]["ValidateUser_Result"]["return_value"])));
Morgana
  • 337
  • 8
  • 19
  • Can you post what you have tried? – SPlatten Jan 16 '19 at 09:15
  • `response["Soap:Envelope"]["Soap:Body"].ValidateUser_Result.return_value` edit: fixed –  Jan 16 '19 at 09:16
  • @ChrisG, you forgot "Soap:Envelope" – SPlatten Jan 16 '19 at 09:16
  • 1
    It's a Javascript object, not JSON. If you need to know how to get values from it, see any Javascript tutorial for beginners, like https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Basics . – RemcoGerlich Jan 16 '19 at 09:17
  • 1
    Possible duplicate of [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – str Jan 16 '19 at 09:18
  • 2
    It should be noted that the above code snippet shows a JavaScript Object literal. JSON is a textual format. There's no such thing as a JSON Object. –  Jan 16 '19 at 09:18

2 Answers2

1
var o = {  
   "Soap:Envelope":{  
      "_attributes":{  
         "xmlns:Soap":"http://schemas.xmlsoap.org/soap/envelope/"
      },
      "Soap:Body":{  
         "ValidateUser_Result":{  
            "_attributes":{  
               "xmlns":"urn:microsoft-dynamics-schemas/codeunit/UserValidation"
            },
            "return_value":{  
               "_text":"P01"
            }
         }
      }
    }
};

var val = o['Soap:Envelope']['Soap:Body']['ValidateUser_Result']['return_value']['_text'];

console.log(val);
TKoL
  • 13,158
  • 3
  • 39
  • 73
1

This will do the trick.

const data = {  
   "Soap:Envelope":{  
      "_attributes":{  
         "xmlns:Soap":"http://schemas.xmlsoap.org/soap/envelope/"
      },
      "Soap:Body":{  
         "ValidateUser_Result":{  
            "_attributes":{  
               "xmlns":"urn:microsoft-dynamics-schemas/codeunit/UserValidation"
            },
            "return_value":{  
               "_text":"P01"
            }
         }
      }
    }
}
console.log(data["Soap:Envelope"]["Soap:Body"]["ValidateUser_Result"]["return_value"]["_text"]);
holydragon
  • 6,158
  • 6
  • 39
  • 62