Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Commonmark migration
Source Link

Here's what is happening with your code: you're accessing the first element of a JSON string, so my guess is you will get its first character: [.

You need to convert your string into an actual array before accessing it!

Use JSON.parse:

The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string.

- MDN web docs

For example you can do:

const json = '[[{"firstname": "Micheal", "lastname": "Brown"}], [{"car": "Ford", "model": "Fiesta"}]]'
// that's what you have
console.log(json[0])
// that's what you want
array = JSON.parse(json)
console.log(array[0])

Here's what is happening with your code: you're accessing the first element of a JSON string, so my guess is you will get its first character: [.

You need to convert your string into an actual array before accessing it!

Use JSON.parse:

The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string.

- MDN web docs

For example you can do:

const json = '[[{"firstname": "Micheal", "lastname": "Brown"}], [{"car": "Ford", "model": "Fiesta"}]]'
// that's what you have
console.log(json[0])
// that's what you want
array = JSON.parse(json)
console.log(array[0])

Here's what is happening with your code: you're accessing the first element of a JSON string, so my guess is you will get its first character: [.

You need to convert your string into an actual array before accessing it!

Use JSON.parse:

The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string.

- MDN web docs

For example you can do:

const json = '[[{"firstname": "Micheal", "lastname": "Brown"}], [{"car": "Ford", "model": "Fiesta"}]]'
// that's what you have
console.log(json[0])
// that's what you want
array = JSON.parse(json)
console.log(array[0])

added 323 characters in body
Source Link
Ivan
  • 41.4k
  • 9
  • 78
  • 120

Here's what is happening with your code: you're accessing the first element of a JSON objectstring, so my guess is you will get its first character: [.

You need to convert your objectstring into an actual array before accessing it!

Use JSON.parse, forJSON.parse :

The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string.

- MDN web docs

For example you can do:

const json = '[[{"firstname": "Micheal", "lastname": "Brown"}], [{"car": "Ford", "model": "Fiesta"}]]'
// that's what you have
console.log(json[0])
// that's what you want
array = JSON.parse(json)
console.log(array[0])

Here's what is happening with your code: you're accessing the first element of a JSON object, so my guess is you will get its first character: [.

You need to convert your object into an actual array before accessing it!

Use JSON.parse, for example you can do:

const json = '[[{"firstname": "Micheal", "lastname": "Brown"}], [{"car": "Ford", "model": "Fiesta"}]]'
// that's what you have
console.log(json[0])
// that's what you want
array = JSON.parse(json)
console.log(array[0])

Here's what is happening with your code: you're accessing the first element of a JSON string, so my guess is you will get its first character: [.

You need to convert your string into an actual array before accessing it!

Use JSON.parse :

The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string.

- MDN web docs

For example you can do:

const json = '[[{"firstname": "Micheal", "lastname": "Brown"}], [{"car": "Ford", "model": "Fiesta"}]]'
// that's what you have
console.log(json[0])
// that's what you want
array = JSON.parse(json)
console.log(array[0])

Source Link
Ivan
  • 41.4k
  • 9
  • 78
  • 120

Here's what is happening with your code: you're accessing the first element of a JSON object, so my guess is you will get its first character: [.

You need to convert your object into an actual array before accessing it!

Use JSON.parse, for example you can do:

const json = '[[{"firstname": "Micheal", "lastname": "Brown"}], [{"car": "Ford", "model": "Fiesta"}]]'
// that's what you have
console.log(json[0])
// that's what you want
array = JSON.parse(json)
console.log(array[0])

default

AltStyle によって変換されたページ (->オリジナル) /