• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

New Here ,
Sep 24, 2010 Sep 24, 2010

Copy link to clipboard

Copied

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

TOPICS
Advanced techniques

Views

10.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Engaged , Sep 24, 2010 Sep 24, 2010

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

Thanks

Votes

Translate

Translate
New Here ,
Sep 24, 2010 Sep 24, 2010

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 24, 2010 Sep 24, 2010

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 24, 2010 Sep 24, 2010

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 24, 2010 Sep 24, 2010

Copy link to clipboard

Copied

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

Thanks

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 24, 2010 Sep 24, 2010

Copy link to clipboard

Copied

Oh jeez - I missed that first email of yours.  I tried the bracket notation and that worked perfectly!

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

Thanks!!

Mike

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 25, 2010 Sep 25, 2010

Copy link to clipboard

Copied

No problem--glad it worked out.

As a follow-up to this, I'd encourage you to keep the bracket notation in mind during future development.  It is EXTREMELY useful in a lot of situations.

For example, if you're building a structure on the fly with dynamic keys, bracket notation is extremely helpful:

     <cfset authors = structnew()>

     <cfloop list="authorlist" index="name">

          <cfset authors[name] = getbooklist(name)>

     </cfloop>

Also, bracket notation is really nice when creating json from ColdFusion structures.  Consider this:

     <cfset author = structnew()>

     <cfset author.name = "Neil Gaiman">

     <cfset author.genre = "Fantasy">

     <cfset author.awesome = true>

     <cfset authorjson = serializejson(author)>

By default, the serialized json produced will have all UPPER CASE keys.  Not a big deal in most cases, but if you're returning the json to something (like a JS library) that is expecting keys in a certain case, it can be a problem.  Fortunately, bracket notation allows you to specify exactly what case you want the structure's keys to be in:

     <cfset author = structnew()>

     <cfset author['name'] = "Neil Gaiman">

     <cfset author['Genre'] = "Fantasy">

     <cfset author['AWESOME'] = true>

     <cfset authorjson = serializejson(author)>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 25, 2010 Sep 25, 2010

Copy link to clipboard

Copied

LATEST

Yeah - I was wondering if there was another type of notation, but I couldn't find it anywhere.  Thanks again!!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 24, 2010 Sep 24, 2010

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation