Skip to main content
September 25, 2010
Answered

How to access a JSON structure key that starts with a number

  • September 25, 2010
  • 2 replies
  • 11821 views

I've got an odd issue here.  I'm accessing someone else's JSON structure so I am not able to change it.  The JSON structure keys are named with both characters and numbers like this:

0198456
product_id198456
1Rashaan Houston feat Tony Loreto
artistRashaan Houston feat Tony Loreto

So, in other words, there's a key named "0" and a key named "product_id", both of which contain the same key.  Why they did that, I don't know, but they did.

In one of the instances, the data stored under the text-named key isn't the same as the numerical-named key.  I tried to output the data in the numerical key like this (it's an array of structures):

#strStompyJSON.data[1].0#

but I get the following error:

--

Invalid CFML construct found on line 41 at column 31.

ColdFusion was looking at the following text:

.0

--

When I do the following, it works fine:

#strStompyJSON.data[1].product_id#

So it looks as though CF doesn't like accessing a variable that starts with a number.  I am able to loop through the structure using a collection loop, and it outputs all of the keys (including the numerical ones) and their values using the following code:

<cfloop collection="#strStompyJSON.data[1]#" item="key">
#key#: #strStompyJSON.data[1][key]#<br />
</cfloop>

However, that doesn't allow me a way to access specific keys named with a number.  Is there a way that I can specifically access a key that is named with a number?

thanks!

Mike

This topic has been closed for replies.
Correct answer existdissolve

Well, it was date that I needed to use, but sometimes the two versions don't match up for some reason or another.

Any ideas how to access the numerical values?

thanks!

Mike


Did you try the bracket notation that I suggested in my first response?  In my tests with your code, it worked without issue.

Thanks

2 replies

existdissolve
Inspiring
September 25, 2010

With ColdFusion structures, you can access keys with dot or bracket notation.  As you've discovered, using dot notation doesn't work so well when the key is a number.

So just use bracket notation.

Instead of:

#strStompyJSON.data[1].0#

Try:

#strStompyJSON.data[1][0]#

This looks exactly like the collection loop you posted, and that's because this is really what the collection loop is doing anyway--the only difference is that in this example you've explicitly specified the value of the key instead of having CF iterate through all the keys.

Hope this helps!

Message was edited by: existdissolve

September 25, 2010

I made an information page which might be helpful so you can see all the data:

http://cfdev.cota.csulb.edu:8080/stompy/json.cfm

all of the data is live data.  Let me know if you have any questions

existdissolve
Inspiring
September 25, 2010

Just out of curiosity, why exactly do you need to reference the numbered keys?  From the looks (a quick look, mind you) of the data that is returned, there appears to be a pretty solid correlation between the numbered keys and the labeled keys.

For example, data[0] *appears* to line up with data['image'], data[1] *appears* to line up with data['artist'], and so on.  If these are consistently the same across the data set returned, why try to reference the numeric keys as opposed to the labeled keys?

[Sorry, after posting this I noticed that you laid these out in the link you provided.]

Message was edited by: existdissolve

September 25, 2010

Well, it was date that I needed to use, but sometimes the two versions don't match up for some reason or another.

Any ideas how to access the numerical values?

thanks!

Mike