...

View Full Version : is my json structure correct?



bortuzar
12-14-2008, 01:51 PM
Hi guys,
Im new to Json.

Im trying to create a good structure that can handle currency exchange history, so i can later create a line graph with the data.

Does this look any good? or do you have any better suggestion.



{
'from': 'CLP',
'to': 'USD',
'history': [
{
'Date': '2008-12-04',
'Rate': 659.615384615
}{
'Date': '2008-12-05',
'Rate': 661.544920235
}{
'Date': '2008-12-06',
'Rate': 659.529806885
}
]
}




thanks

itsallkizza
12-14-2008, 10:16 PM
use double quotes, other than that, looks good

v08i
12-15-2008, 06:50 AM
use commas when you are separating objects in "history". rest is fine. also follow itsallkizza's advice


{
'from': 'CLP',
'to': 'USD',
'history': [
{
'Date': '2008-12-04',
'Rate': 659.615384615
},
{
'Date': '2008-12-05',
'Rate': 661.544920235
},
{
'Date': '2008-12-06',
'Rate': 659.529806885
}
]
}

read more about json (http://www.vijayjoshi.org/2008/08/20/what-is-json-definition-and-uses-in-javascript/)

thanks
Vijay

itsallkizza
12-15-2008, 08:26 PM
Good call, didn't notice the missing commas :)

...And that's why I spend hours debugging my code.

rnd me
12-15-2008, 09:09 PM
if you want to "validate" json, simply paste it into firebug and click "run". if the console says "object" in green, your json is fine. If it's not ok, the error is reported in red.

bonus tip: if the error info is not helpful, paste your json in between "<script>" and "</script>" in the prepage link in my signature, and you get extended error info, including the line number of the error. (firefox only)

itsallkizza
12-15-2008, 09:18 PM
Ah, but that would validate any object would it not? JSON objects are further restricted by strings/objects/arrays as data types and double quotes as encapsulators.

rnd me
12-15-2008, 09:50 PM
Ah, but that would validate any object would it not? JSON objects are further restricted by strings/objects/arrays as data types and double quotes as encapsulators.

yes, but it sure would've caught the missing commas that you missed... ;^)

as a minor point, most json i've encountered is not JSON, single quotes are used, etc.

I guess instead of "validate", i should have said "validate for JS". if you want to use the JSON in an a browser, firebug will tell you if it works. if you need inter-machine compatibility, you may have to be more strict.

if it won't work in firebug, it won't work at all. if it works in firebug, it will work on all browsers, and MAYBE other places as well.

A1ien51
12-16-2008, 03:24 PM
Paste the JSON into the textbox in this page: http://www.jsonlint.com/

It will yell at you until it is correct. [Note it will demand you use " instead of ' for strings which was already mentioned]

Eric

Kor
12-17-2008, 02:41 PM
As far as I know, in JSON it does not matter whether you use double quotes or single quotes to mark the strings (same as in javascript parent language). Never heard about any double quotes restriction.

A1ien51
12-17-2008, 07:01 PM
As far as I know, in JSON it does not matter whether you use double quotes or single quotes to mark the strings (same as in javascript parent language). Never heard about any double quotes restriction.

Yes it is valid to use ' for strings in JavaScript and a JSON object will not freak out if you use ' in it. Heck you do not even need the quotes if it is the name portion of the name value pair.

The "JSON Standard" requires that we use double quotes to define a string. It is little silly, but it does make it easier to validate the syntax.

Eric

itsallkizza
12-17-2008, 07:05 PM
JSON itself is just a standard, you can use other datatypes other than objects arrays and strings as well - anything that functions as an object in Javascript can be passed as a string via ajax. JSON is just a "standard" structure for doing so.

Kor
12-18-2008, 07:31 AM
Well, if you have single quotes inside the string, you can simply escape them. The situation is the same if you have double quotes inside a double quoted string. Javascript makes no difference between the two situations, thus there is no reason for JSON in Javascript should act otherwise.

In fact it does not matter. And double quotes are better, indeed, as they are easier to be seen by human eyes as string delimiters within a JSON long structure :)

vikasg
02-17-2009, 08:16 AM
This is the proper structure of your json code.
{
"from": "CLP",
"to": "USD",
"history": [
{
"Date": "2008-12-04",
"Rate": 659.615384615
},
{
"Date": "2008-12-05",
"Rate": 661.544920235
},
{
"Date": "2008-12-06",
"Rate": 659.529806885
}
]
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum