3

I want to create the following JSON object, as seen from a console log:

Object
    .   member: Object
    .   id: 8286

I've been trying:

'member' :[{'id': 8286}]

but get the following error: "Uncaught SyntaxError: Unexpected token :"

What am I doing wrong? Thanks

AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012
  • http://bonsaiden.github.com/JavaScript-Garden/ – Zirak Jan 06 '12 at 21:29
  • 4
    I don't upvote to compensate downvotes, but I should. This is a perfectly valid question. – Julio Santos Jan 06 '12 at 21:29
  • 1
    @JúlioSantos, that's a good rule, though. You might think the question deserves to be at zero, but you are giving the requestor +8 rep. In this case, that's probably fine, but in general it often rewards bad or mediocre questions. – Devin Burke Jan 06 '12 at 21:32
  • Try to you something like http://www.jshint.com/ – jake Jan 06 '12 at 21:32
  • @JúlioSantos I wonder if the title is a bit more generic thus misleading (or tempting) me downvote the question under the "have not researched properly" category. – jake Jan 06 '12 at 21:37
  • 3
    Do you want to create JSON or a JavaScript object? Have you read a [JavaScript Guide](https://developer.mozilla.org/en/JavaScript/Guide) at all? – Felix Kling Jan 06 '12 at 21:38
  • @JúlioSantos it's a valid question like "how to assign `1` to a variable" is a valid question -- perfectly valid but come on. – Esailija Jan 06 '12 at 21:39
  • @jake, Esailija: There are plenty of "come on" questions on SO, and that's part of what makes the community grow. Take this one, for instance — http://stackoverflow.com/q/178325/214773 — it's so painfully simple, and yet look at the number of views, and upvotes both on the question and the answers. IMO, we need simple questions to drive new users to the site, because that's what they'll be googling for. – Julio Santos Jan 06 '12 at 22:04
  • @jake I agreed with you. Just suggesting a probable cause for the confusion. Maybe a "Is there something wrong with my JSON object initialization" have pushed this more to programming error question category. I am not sure if I should change the title. – jake Jan 06 '12 at 22:14

4 Answers4

6
var member = {
    id: 8286
};

this allows you to access it like

member.id

You could have also meant something like:

var data = {
    member: {
        id: 8286
    }
};

which you would access like so:

data.member.id

If this is not what you want, please clarify in your post cause I'm not sure I'm following your request correctly.

Kai Qing
  • 18,793
  • 5
  • 39
  • 57
3

You're missing the curly braces surrounding the object. As in:

var x = {'member': [{'id':8286}]};
Dan Suceava
  • 157
  • 3
1

You mean something like this?

{
    "member": {},
    "id": 8286
}
fivedigit
  • 18,464
  • 6
  • 54
  • 58
0
{
   "member":{
      "id":value
   },
   {
      another_data:value
   }
}

the data are accesses using the(.) operator. the id is accessed by member.id

Rashad
  • 11,057
  • 4
  • 45
  • 73