2

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

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
Joe
  • 4,852
  • 10
  • 63
  • 82

3 Answers3

3

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'
    ]
};
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
2

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 =

Douglas
  • 36,802
  • 9
  • 76
  • 89
locrizak
  • 12,192
  • 12
  • 60
  • 80
1

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"
    ]
};
mcgrailm
  • 17,469
  • 22
  • 83
  • 129
  • 1
    No, the first is not JSON, it's a Javascript literal object. JSON is a text format for describing objects. – Guffa Apr 17 '11 at 15:17
  • @Guffa what would you define json as in n example ? – mcgrailm Apr 17 '11 at 15:25
  • 3
    @mcgrailm: This is an example of JSON: `{"item":"value","item2":"value"}`, and this is an example of something that is not JSON but Javascript: `var foo = {"item":"value","item2":"value"};`. – Guffa Apr 17 '11 at 17:50
  • @guffa so putting the json into a variable makes it a javascript object literal isn't that splitting hairs ? you really think my answer is worthy of a downvote ? – mcgrailm Apr 17 '11 at 17:53
  • 3
    @mcgrailm: You are missing the point completely. JSON is not Javascript code, it's a text format. As the JSON syntax is a subset of the Javascript syntax, you can take JSON and put it in Javascript code, but then it's not JSON any more, because it's not data any more, it's code. There is no such thing as a "JSON object". – Guffa Apr 17 '11 at 19:21
  • Agreed with Guffa, these examples would be better as JSON rather than JS scripts, then they could be copy-pasted whole into eg JSONLint and pass validation. – Douglas Apr 17 '15 at 13:20