JSON format failing on JSONLint

2.2k Views Asked by At

I'm writing some functions that construct what should be a properly formatted JSON string which I can then parse into a JSON object.

My object is failing on JSONLint with the following error:

syntax error, unexpected TINVALID at line 2

[
    SERVER: {
        ip = 10.10.10.1,
        port = 8100
    },
    TYPES: [
        ssh,
        shht
    ]
]

I assumed that this would give me an array of JavaScript objects.

Even if I did this instead (object instead of array):

{
    SERVER: {
        ip = 10.10.10.1,
        port = 8100
    },
    TYPES: [
        ssh,
        shht
    ]
}

It doesn't work and I get the same error:

I assume that with the object, I would be able to access some data like so:

var serverIP = myObject.SERVER.ip;

That's certainly what I'd like to do.

Many thanks in advance,

Joe

3

There are 3 best solutions below

6
On BEST ANSWER

your mixing json object with javascript arrays

this is json format

{
    "item":"value",
    "item2":"value"
}

and this would be a JavaScript array

[
    "apple",
    "orange"
]

os I think this is what you are looking for

{
    "SERVER": {
        "ip": "10.10.10.1",
        "port": 8100
    },
    "TYPES": [
        "ssh",
        "shht"
    ]
};
0
On

This validates:


{
    "SERVER": {
        "ip" : "10.10.10.1",
        "port" : 8100 
    },
    "TYPES": [
        "ssh",
        "shht" 
    ]
}

you need double quotes around every string and since its an object you need : instead of =

0
On

You have to use quotation marks around the identifiers, and the values that are strings:

This is valid JSON:

{
    "SERVER": {
        "ip": "10.10.10.1",
        "port": 8100
    },
    "TYPES": [
        "ssh",
        "shht"
    ]
}

If you are actually not using JSON at all, but just trying to create a Javascript literal object, then you should not use JSONLint to check the code.

This is valid Javascript:

var myObject = {
    SERVER: {
        ip: '10.10.10.1',
        port: 8100
    },
    TYPES: [
        'ssh',
        'shht'
    ]
};