473,405 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

question about valid keys in JSON

var Functions = {
myfunction : function() { }

}

var refToFunction = Functions.myfunction;

var myJsonObj1 = {
refToFunction : "some value"
}
var myJsonObj2 = {
Functions.myfunction: "some value"
}
Can anyone explain why my browser complains when creating the second
myJsonObj2 (missing : after property id
[Break on this error] Functions.myfunction : "some value" \n)? Can I
not have dot notation in a JSON key?

thanks,

-Morgan

Aug 20 '08
3 2556
On Aug 20, 9:15 pm, Morgan Packard wrote:
var Functions = {
myfunction : function() { }

}

var refToFunction = Functions.myfunction;

var myJsonObj1 = {
refToFunction : "some value"

}

var myJsonObj2 = {
Functions.myfunction: "some value"

}

Can anyone explain why my browser complains when creating
the second myJsonObj2 ...
<snip>

The property names used in an object literal may only be Identifiers,
string literals or numeric literals (and numeric literals don't work
in some implementations (e.g. on Mac IE 5)).
>? Can I not have dot notation in a JSON key?
A property accessor is none of an Identifier, a string literal or a
numeric literal, so no you cannot. But if you want a dot in the
property name use a string literal; - {"Functions.myfunction": "some
value"} - is a legal object literal. Indeed, strictly JSON requires
the property names used to be quoted else what you have is just a
javascript object literal and not JSON at all.
Aug 20 '08
Morgan Packard <he****************@gmail.comwrites:

....
var myJsonObj2 = {
Functions.myfunction: "some value"
}
Can anyone explain why my browser complains when creating the second
myJsonObj2 (missing : after property id
[Break on this error] Functions.myfunction : "some value" \n)? Can I
not have dot notation in a JSON key?
Indeed, you cannot have a dot.

In fact, in JSON all keys *must* be quoted. I.e.:
{"refToFunction" : "some value", "Functions.myfunction": "some value"}
Many parsers for JSON will ignore this constraint, especially those using
Javascript's "eval" function.

In Javascript, keys that are also valid identifiers or number literals
may be left unquoted:
{refToFunction: "some value", "Functions.myfunction": "some value",
2: "some value", true: "some value"}

/L
--
Lasse Reichstein Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Aug 20 '08
Morgan Packard wrote:
var Functions = {
myfunction : function() { }

}
In addition to the information you already got, JSON doesn't allow
function expressions for values.

JSON uses a subset of Object Literal Notation. For more details, please
see: http://www.json.org/

Garrett
thanks,

-Morgan
Aug 21 '08

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sasha | last post by:
Hi everybody, I would like to hear your thoughts on the following problem. We have the following classes. Class Exam int ID* int Version* string Name
3
by: Sasha | last post by:
Hi everybody, I would like to hear your thoughts on the following problem. We have the following classes. Class Exam int ID* int Version* string Name
4
by: Tiraman | last post by:
Hi , can some one explain me what does it mean those 2 params that i need to pass the des.CreateEncryptor(rgbKey as byte,rgbIV as byte) Best Regards , Tiraman :-)
2
by: Curtis.DanielN | last post by:
If you have an object list ex: var olist = {"daniel":4, "Tom":5}; Is there any way to reference the objects fields w/o knowing their names? Say for example I pass the list to another function...
5
by: Tom Cole | last post by:
Let's say I have the following JSON string returned from a server-side process: { values: } I then create a JSON object from it using eval() (tell me if this is not what should be done). ...
2
by: eggie5 | last post by:
Is this JSON valid? I would like to access it like this in my javascript: var json=eval ('('+json+')'); json.devices.modelNumber and json.devices.image Would this work?
2
by: ChrisO | last post by:
I've been pretty infatuated with JSON for some time now since "discovering" it a while back. (It's been there all along in JavaScript, but it was just never "noticed" or used by most until...
2
by: msoulier | last post by:
I'm having a problem composing valid json, and I'm not sure what I'm doing wrong. I have a couple of name/value pairs to pass back, plus an array of objects. I'm trying to encode them like so. ...
9
by: BryanA | last post by:
Is it possible to parse JSON data and then post it to a mysql db with the data in their respective fields?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.