How do I get json data from this array

Discussion in 'JavaScript' started by ketting00, Jan 11, 2020.

  1. #1
    Hi Guy,

    I've this simple data and I want to use it:
    [ {id: 1, name: 'John Smith' } ]
    Code (markup):
    So how do I get the id or the name out of this set of data.

    Thank you,
     
    ketting00, Jan 11, 2020 IP
  2. ketting00

    ketting00 Well-Known Member

    Messages:
    782
    Likes Received:
    28
    Best Answers:
    3
    Trophy Points:
    128
    #2
    Ok, I get it.
    var data = [ {id: 1, name: 'John Smith' } ];
    console.log(data[0].name);
    
    Code (markup):
     
    ketting00, Jan 11, 2020 IP
    scu8a likes this.
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #3
    The outer "mandatory" array of JSON often throws people like that.
     
    deathshadow, Jan 12, 2020 IP
  4. loki781

    loki781 Greenhorn

    Messages:
    25
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    13
    #4
    It's not mandatory, the only JSON there is within {}. The outer brackets are an array, typically sent like that because they receive an array of JSON items.
     
    loki781, Apr 12, 2020 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #5
    Hence the quotes... for some reason a LOT of people seem to add it and claim you have to. Yes, it makes no sense.

    I should have implied a more sarcastic tone.
     
    deathshadow, Apr 12, 2020 IP