0

Here is my JSON code and as u can see first object name "com.app.connect.model.Login" contain periods.

{
  "com.app.connect.model.Login": {
    "status": "FAIL",
    "message": "Incorrect username or password."
  }
}

I want to extract "status" value. I can get "status" value if object name doesn't contain period.

var statusJson2 = jsonCC2.comappconnectmodelLoginStatus.status;

I tried many things but I couldn't achieve.

var jsonCC = { "com.app.connect.model.LoginStatus": { "status": "FAIL", "message": "Incorrect username or password." } };

var jsonCC2 = { "comappconnectmodelLoginStatus": { "status": "FAIL", "message": "Incorrect username or password." } };

//var statusJson = jsonCC.["com.app.connect.model.LoginStatus"].status;

var statusJson2 = jsonCC2.comappconnectmodelLoginStatus.status;

//console.log(statusJson)

console.log(statusJson2)

Any suggestion for this?

Erdogan
  • 65
  • 2
  • 15
  • 2
    Possible duplicate of [How to get JSON objects value if its name contains dots?](http://stackoverflow.com/questions/2577172/how-to-get-json-objects-value-if-its-name-contains-dots) – Pat Aug 16 '16 at 16:48
  • @Pat I saw this question and answer. I tried it too but the example is array and I couldn't apply for this code. – Erdogan Aug 16 '16 at 16:56

1 Answers1

5

You can just do:

var subObject = jsonCC["com.app.connect.model.Login"];

Don't put a period between jsonCC and [

Here is a JSFiddle of the working code.

kemiller2002
  • 113,795
  • 27
  • 197
  • 251
  • I tried but I'm getting error : Cannot read property 'com.app.connect.model.LoginStatus' of undefined – Erdogan Aug 16 '16 at 16:55
  • @ObsessiO Your JSON is just `com.app.connect.mod‌​el.Login` – mafafu Aug 16 '16 at 16:55
  • yes I made typo mistake when I carry code to stackoverflow. When I removed period between `jsonCC` and `[` works fine. Thank you. – Erdogan Aug 16 '16 at 17:01