Home > Software design >  JSON not indexing correctly
JSON not indexing correctly

Time:01-07

I have this JSON:

[
  {
    "type": "GUILD_TEXT",
    "deleted": false,
    "guild": "898666651547996200",
    "guildId": "898666651547996200",
    "parentId": "903388176100495390",
    "permissionOverwrites": [
      "900991433576689675",
      "917426278003523604",
      "898666651547996200",
      "898825198709641246"
    ],
    "messages": [
      "928781911982219307"
    ],
    "threads": [],
    "nsfw": false,
    "id": "903388255528042566",
    "name": "updates",
    "rawPosition": 41,
    "topic": null,
    "lastMessageId": "928781911982219307",
    "rateLimitPerUser": 0,
    "createdTimestamp": 1635454944260
  }
]

(call = the json)

Shouldn't this be returning "updates": call[0]["name"]

Via JS, it is returning undefined.

call[0] is returning as {

I've tried it on various other languages and it has been working as intended... just not in JS.

CodePudding user response:

It does work:

const call = [{
  "type": "GUILD_TEXT",
  "deleted": false,
  "guild": "898666651547996200",
  "guildId": "898666651547996200",
  "parentId": "903388176100495390",
  "permissionOverwrites": [
    "900991433576689675",
    "917426278003523604",
    "898666651547996200",
    "898825198709641246"
  ],
  "messages": [
    "928781911982219307"
  ],
  "threads": [],
  "nsfw": false,
  "id": "903388255528042566",
  "name": "updates",
  "rawPosition": 41,
  "topic": null,
  "lastMessageId": "928781911982219307",
  "rateLimitPerUser": 0,
  "createdTimestamp": 1635454944260
}]

console.log(call[0]["name"]) // updates

const callString = JSON.stringify(call)
console.log(JSON.parse(callString)[0]["name"]) // updates

CodePudding user response:

I tried

var name= call[0].name;

and

var name= call[0]["name"];

evertything is working properly returning "updates".

  •  Tags:  
  • Related