1

So I'm trying to put together some JSON and parse it out into jquery but I am doing something wrong with my JSON syntax. I tried running it through a validator but it doesn't really tell me what I'm doing wrong. Can somebody point out the error of my ways?

var searchresults = [
{
    "providerlisting": [
    {
        "nametitle": "Cory M Spears, MD, FACP",
        "caretype": "Internal Medicine",
        "preferredProvider": true,
        "address1": "289 N. Highland Ave.",
        "address2": "",
        "cityStateZip": "Atlanta, GA 30306",
        "coverage": "/images/example.png",
        "status": "Out of Network",
        "psn": "",
        "dcontact": "urlhere",
        "save": "urlhere",
        "rating": "urlhere",
    },
    {
        "nametitle": "Jimmy Dean, MD, FACP",
        "caretype": "External Medicine",
        "preferredProvider": false,
        "address1": "3 Piedmont Rd.",
        "address2": "",
        "cityStateZip": "Atlanta, GA 30706",
        "coverage": "/images/example2.png",
        "status": "In Network",
        "psn": "urlhere",
        "dcontact": "urlhere",
        "save": "urlhere",
        "rating": "urlhere",
    },
    {
        "nametitle": "John Doe, DD, PM",
        "caretype": "Internal Medicine",
        "preferredProvider": true,
        "address1": "500 Ponce De Leon Ave",
        "address2": "Suite 5",
        "cityStateZip": "Atlanta, GA 30706",
        "coverage": "/images/example2.png",
        "status": "Out of Network",
        "psn": "urlhere",
        "dcontact": "urlhere",
        "save": "urlhere",
        "rating": "urlhere",
    }]
},
{
"categories": [{
    "categoryMenu": [
        {
            "providertype": [
                {
                    "title": "Doctor",
                    "link": "#doctor",
                    "amount": "400"
                },
                 {
                     "title": "Hospital",
                     "link": "#hospital",
                     "amount": "40"
                 },
                 {
                     "title": "Urgent Care",
                     "link": "#urgentCare",
                     "amount": "37"
                 }
            ]
        },
        {
            "specialty": [
                {
                    "title": "Allergy and Immunology",
                    "link": "#allergyAndImmunology",
                    "amount": "2"
                },
                {
                    "title": "Audiology",
                    "link": "#audiology",
                    "amount": "3"
                },
                {
                    "title": "Allergy and Immunology",
                    "link": "#allergyAndImmunology",
                    "amount": "6"
                },
                {
                    "title": "Ambulatory Surgical Center",
                    "link": "#ambulatorySurgicalCenter",
                    "amount": "4"
                }
            ]
        },
        {
            "gender": [
                {
                    "title": "Male",
                    "link": "#male",
                    "amount": "67"
                },
                {
                    "title": "Female",
                    "link": "#female",
                    "amount": "3"
                }
            ]
        }
    }]
}];
2
  • 1
    jsonlint clearly reports an error on line 16. There's a stray comma. That's an error in real JSON, but that's not what you posted; you posted a JavaScript statement, and it wouldn't be an error in JavaScript (except in older IE versions). Commented Sep 4, 2013 at 16:41
  • What you currently have is a script that assigns a huge object literal to a variable. If you want JSON (what the validator validates), you would need to take only the literal without its surroundings. Commented Sep 4, 2013 at 16:42

4 Answers 4

4

Remove the , at the end of each

 "rating": "urlhere"

there was a ] missing on the third last line, below is a valid json object

var searchresults = [{
    "providerlisting": [{
        "nametitle": "Cory M Spears, MD, FACP",
            "caretype": "Internal Medicine",
            "preferredProvider": true,
            "address1": "289 N. Highland Ave.",
            "address2": "",
            "cityStateZip": "Atlanta, GA 30306",
            "coverage": "/images/example.png",
            "status": "Out of Network",
            "psn": "",
            "dcontact": "urlhere",
            "save": "urlhere",
            "rating": "urlhere"
    }, {
        "nametitle": "Jimmy Dean, MD, FACP",
            "caretype": "External Medicine",
            "preferredProvider": false,
            "address1": "3 Piedmont Rd.",
            "address2": "",
            "cityStateZip": "Atlanta, GA 30706",
            "coverage": "/images/example2.png",
            "status": "In Network",
            "psn": "urlhere",
            "dcontact": "urlhere",
            "save": "urlhere",
            "rating": "urlhere"
    }, {
        "nametitle": "John Doe, DD, PM",
            "caretype": "Internal Medicine",
            "preferredProvider": true,
            "address1": "500 Ponce De Leon Ave",
            "address2": "Suite 5",
            "cityStateZip": "Atlanta, GA 30706",
            "coverage": "/images/example2.png",
            "status": "Out of Network",
            "psn": "urlhere",
            "dcontact": "urlhere",
            "save": "urlhere",
            "rating": "urlhere"
    }]
}, {
    "categories": [{
        "categoryMenu": [{
            "providertype": [{
                "title": "Doctor",
                    "link": "#doctor",
                    "amount": "400"
            }, {
                "title": "Hospital",
                    "link": "#hospital",
                    "amount": "40"
            }, {
                "title": "Urgent Care",
                    "link": "#urgentCare",
                    "amount": "37"
            }]
        }, {
            "specialty": [{
                "title": "Allergy and Immunology",
                    "link": "#allergyAndImmunology",
                    "amount": "2"
            }, {
                "title": "Audiology",
                    "link": "#audiology",
                    "amount": "3"
            }, {
                "title": "Allergy and Immunology",
                    "link": "#allergyAndImmunology",
                    "amount": "6"
            }, {
                "title": "Ambulatory Surgical Center",
                    "link": "#ambulatorySurgicalCenter",
                    "amount": "4"
            }]
        }, {
            "gender": [{
                "title": "Male",
                    "link": "#male",
                    "amount": "67"
            }, {
                "title": "Female",
                    "link": "#female",
                    "amount": "3"
            }]
        }]
    }]
}];
Sign up to request clarification or add additional context in comments.

6 Comments

When I threw it into Notepad++ and formatted it with JSMin, it also looks like he's missing a closing square bracket ] at the end
@valverij yes indeed, added the full working copy as it would be hard to point out which line, etc
Thanks, it still isn't running through jsonlint.com though. I'm guessing it doesn't like the javascript. Where should I copy out and end copy in order to get it to run correctly through the validator?
@Chad it passes on JSONLint, that is a json object validator, not javascript validator, to test it there, you would need to remove the variable declaration/assignment and the ; at the end
so start with { "providerlisting": [{... ?
|
0

There are only , commas after a key/value pairs in objects if there is another one following it. (Same goes for Arrays as well)

For example:

var a = {
   key  : 'value',
   keyB : 'value'   // <-- there is no trailing comma before an object ends
};

Comments

0

Do you know http://jsonlint.com/?

You have to realise, an object in javascript is nearly the same like an associative array.

I think you should read a bit more about Objects and Arrays in Javascript.

Try this:

var searchresults = {
        "providerlisting": [
            {
                "nametitle": "Cory M Spears, MD, FACP",
                "caretype": "Internaenter code herel Medicine",
                "preferredProvider": true,
                "address1": "289 N. Highland Ave.",
                "address2": "",
                "cityStateZip": "Atlanta, GA 30306",
                "coverage": "/images/example.png",
                "status": "Out of Network",
                "psn": "",
                "dcontact": "urlhere",
                "save": "urlhere",
                "rating": "urlhere"
            },
            {
                "nametitle": "Jimmy Dean, MD, FACP",
                "caretype": "External Medicine",
                "preferredProvider": false,
                "address1": "3 Piedmont Rd.",
                "address2": "",
                "cityStateZip": "Atlanta, GA 30706",
                "coverage": "/images/example2.png",
                "status": "In Network",
                "psn": "urlhere",
                "dcontact": "urlhere",
                "save": "urlhere",
                "rating": "urlhere"
            },
            {
                "nametitle": "John Doe, DD, PM",
                "caretype": "Internal Medicine",
                "preferredProvider": true,
                "address1": "500 Ponce De Leon Ave",
                "address2": "Suite 5",
                "cityStateZip": "Atlanta, GA 30706",
                "coverage": "/images/example2.png",
                "status": "Out of Network",
                "psn": "urlhere",
                "dcontact": "urlhere",
                "save": "urlhere",
                "rating": "urlhere"
            }
        ],
        "categories": {
            "categoryMenu": {
                "providertype": [
                    {
                        "title": "Doctor",
                        "link": "#doctor",
                        "amount": "400"
                    },
                    {
                        "title": "Hospital",
                        "link": "#hospital",
                        "amount": "40"
                    },
                    {
                        "title": "Urgent Care",
                        "link": "#urgentCare",
                        "amount": "37"
                    }
                ],
                "specialty": [
                    {
                        "title": "Allergy and Immunology",
                        "link": "#allergyAndImmunology",
                        "amount": "2"
                    },
                    {
                        "title": "Audiology",
                        "link": "#audiology",
                        "amount": "3"
                    },
                    {
                        "title": "Allergy and Immunology",
                        "link": "#allergyAndImmunology",
                        "amount": "6"
                    },
                    {
                        "title": "Ambulatory Surgical Center",
                        "link": "#ambulatorySurgicalCenter",
                        "amount": "4"
                    }
                ],
                "gender": [
                    {
                        "title": "Male",
                        "link": "#male",
                        "amount": "67"
                    },
                    {
                        "title": "Female",
                        "link": "#female",
                        "amount": "3"
                    }
                ]
            }
        }
    }

Comments

0
Please find the corrected Json string

var searchresults = [
{
    "providerlisting": [
    {
        "nametitle": "Cory M Spears, MD, FACP",
        "caretype": "Internal Medicine",
        "preferredProvider": true,
        "address1": "289 N. Highland Ave.",
        "address2": "",
        "cityStateZip": "Atlanta, GA 30306",
        "coverage": "/images/example.png",
        "status": "Out of Network",
        "psn": "",
        "dcontact": "urlhere",
        "save": "urlhere",
        "rating": "urlhere",
    },
    {
        "nametitle": "Jimmy Dean, MD, FACP",
        "caretype": "External Medicine",
        "preferredProvider": false,
        "address1": "3 Piedmont Rd.",
        "address2": "",
        "cityStateZip": "Atlanta, GA 30706",
        "coverage": "/images/example2.png",
        "status": "In Network",
        "psn": "urlhere",
        "dcontact": "urlhere",
        "save": "urlhere",
        "rating": "urlhere",
    },
    {
        "nametitle": "John Doe, DD, PM",
        "caretype": "Internal Medicine",
        "preferredProvider": true,
        "address1": "500 Ponce De Leon Ave",
        "address2": "Suite 5",
        "cityStateZip": "Atlanta, GA 30706",
        "coverage": "/images/example2.png",
        "status": "Out of Network",
        "psn": "urlhere",
        "dcontact": "urlhere",
        "save": "urlhere",
        "rating": "urlhere",
    }]
},
{
"categories": [{
    "categoryMenu": [
        {
            "providertype": [
                {
                    "title": "Doctor",
                    "link": "#doctor",
                    "amount": "400"
                },
                 {
                     "title": "Hospital",
                     "link": "#hospital",
                     "amount": "40"
                 },
                 {
                     "title": "Urgent Care",
                     "link": "#urgentCare",
                     "amount": "37"
                 }
            ]
        },
        {
            "specialty": [
                {
                    "title": "Allergy and Immunology",
                    "link": "#allergyAndImmunology",
                    "amount": "2"
                },
                {
                    "title": "Audiology",
                    "link": "#audiology",
                    "amount": "3"
                },
                {
                    "title": "Allergy and Immunology",
                    "link": "#allergyAndImmunology",
                    "amount": "6"
                },
                {
                    "title": "Ambulatory Surgical Center",
                    "link": "#ambulatorySurgicalCenter",
                    "amount": "4"
                }
            ]
        },
        {
            "gender": [
                {
                    "title": "Male",
                    "link": "#male",
                    "amount": "67"
                },
                {
                    "title": "Female",
                    "link": "#female",
                    "amount": "3"
                }
            ]
        }
    ]
}]
}];

alert(JSON.stringify(searchresults))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.