Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New to the CF scene
    Join Date
    Dec 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    is my json structure correct?

    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.

    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
            }
        ]
    }

    thanks

  • #2
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    use double quotes, other than that, looks good
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com

  • #3
    New Coder
    Join Date
    Nov 2008
    Location
    New Delhi,India
    Posts
    26
    Thanks
    1
    Thanked 3 Times in 3 Posts

    only one mistake

    use commas when you are separating objects in "history". rest is fine. also follow itsallkizza's advice
    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
            }
        ]
    }
    read more about json

    thanks
    Vijay
    Last edited by v08i; 12-15-2008 at 05:53 AM. Reason: code formatting

  • Users who have thanked v08i for this post:

    itsallkizza (12-15-2008)

  • #4
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    Good call, didn't notice the missing commas

    ...And that's why I spend hours debugging my code.
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,302
    Thanks
    10
    Thanked 586 Times in 567 Posts
    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)
    Last edited by rnd me; 12-15-2008 at 08:12 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #6
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    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.
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com

  • #7
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,302
    Thanks
    10
    Thanked 586 Times in 567 Posts
    Quote Originally Posted by itsallkizza View Post
    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.
    Last edited by rnd me; 12-15-2008 at 08:55 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #8
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    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
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • Users who have thanked A1ien51 for this post:

    oesxyl (12-17-2008)

  • #9
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #10
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Quote Originally Posted by Kor View Post
    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
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #11
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    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.
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com

  • #12
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #13
    New Coder
    Join Date
    Nov 2008
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    }
    ]
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •