views:

27

answers:

2

I am working on a project that requires an object to have multiple values. For instance, a list of 'things' all with a name but each thing also has another category, "Tags" with could include things like round, circle, ball, sport, etc. all words describing what the 'thing' is. I am trying to define this in JSON but I keep running into errors with jsonlint. Take a look:

{
"things":[{
    "name":"thing1",
    "tags":[{"globe","circle","round","world"}]
},
{
    "name":"thing2",
    "tags":[{"cloud","weather","lightning","sky"}]
},
{
    "name":"thing3",
    "tags":[{"round","bullseye","target"}]
},
{
    "name":"thing4",
    "tags":[{"round","key","lock"}]
},
{
    "name":"thing5",
    "tags":[{"weather","sun","sky","summer"}]
}]

}

Hopefully you see what I am trying to accomplish.

Thanks for your help!

Anthony

+2  A: 

Your "tags" objects seem off to me. The code:

[{"globe","circle","round","world"}]

would try to create an array of an object whose properties don't have any values, but I'm not sure that's even valid syntax. Do you want an array of words for "tags?" If so, you need to drop those curly braces:

var myThings = {
    "things":[{
        "name":"thing1",
        "tags":["globe","circle","round","world"]
    }]
};

That would give you an object with property "things" that contains an array of one object with a "name" and a "tags" property, where "tags" is an array of words.

You could access "tags" like so:

var thing1tag2 = myThings.things[0].tags[1]; // = "circle"
Cory Larson
Yes, remove the curly braces from the tags.
Matt Williamson
+1  A: 

If each thing has a name then why not use that name as a tag as follows:

var things = {
  "thing1": ["globe", "circle", "round", "world"],
  "thing2": ["cloud", "weather", "lightning", "sky"],
  "thing3": ["round", "bullseye", "target"],
  "thing4": ["round", "key", "lock"],
  "thing5", ["weather", "sun", "sky", "summer"]
};

Now you can refer to things.thing1 or things.thing2 and the tags as things.thing1[0] and things.thing2[2] etc.

Hans B PUFAL