View Full Version : [SOLVED] JSON response with keys that contain "-" hyphen character ref error

19 May 2010, 7:40 AM
I am receiving a JSON response that contains "-" hyphens in some keys, it decodes okay, but when I attempt to reference any values in the JSON object, I get errors. See example below.

var jsonObj = Ext.util.JSON.decode( response.responseText );

// the jsonObj contains a key md-number
// try to reference the field/value

var mdno = jsonObj.md-number; // throws an error during runtime 'undefined number'

So, my question is how do I reference these keys that contain a hyphen?

PS: I've looked at the jsonObj in Firebug and the object contains all the key/values pairs. So it was decoded without error. I just cannot access keys with "-" in it.

Any help would be appreciated.

19 May 2010, 12:27 PM
Just a guess, but try jsonObj['md-number']

19 May 2010, 12:47 PM
@Keylan: That's correct.

the - is an operator, and will treat it that way. So to access the property, you will need to do what Keylan has suggested.

19 May 2010, 1:21 PM
YES! That did it! Perfect!

Thank you!

19 May 2010, 1:59 PM
Is it [SOLVED]?

19 May 2010, 9:04 PM
Yes, it is solved.

20 May 2010, 6:58 AM
Then I would suggest you edit your original post, and append a prefix of [SOLVED]. Much appreciated.

17 Apr 2013, 5:40 AM
I was facing other problem on how to specify the hyphen in my store root object
My nested data looks like

"data": {
"device-config": {
"device": {
"mac": "00-11-22-33-A8-CA",
"hostname": "Ap228382",
"timezone": "Etc/UTC",
"country_code": "ai"

And my store will read the nested data with hyphen as below

root: 'data["device-config"].device'