Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

251
Visualizações
JSON.parse: unexpected character at line 1 column 2 of the JSON data

I'm trying to make a quiz with HTML, CSS and JavaScript. Now i'm trying to see if the jsonData works but i get this error message but i can't find where the unecpected character is (it says line 1 column 2) but don't see it Other thing am i doing it good? I just started this year with coding

var jsonData = [
    {
        "q1": {
            "question": "what is the Capital city of Australia ",
            "answers": {
                "a": "Melbourne",
                "b": "Sydney",
                "c": "Caneberra",
                "d": "Brisbane"
            },
            "correctAnswers": "c"
        }
    },
]

const me = JSON.parse(jsonData)
console.log(me.correctAnswers);  
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The problems have already been pointed out in the comments by T.J Crowder and Samball, but I wanted to show you the difference between a JavaScript data structure and a JSON string that needs to be parsed so you can better understand their comments.

  • A JSON string needs to be parsed to JavaScript data structure (array, objects, values) for you to be able to work with the data stored in the file properly
  • A JavaScript object/ array etc. that you define in JavaScript is already an JavaScript data structure so no need to parse it. Please note, any valid JSON is also valid JavaScript, but not the other way round.

Here a small program to illustrate what I've just written.

// this needs to be parsed so you can access it as JS data structures
const stringData = `[
  {
      "q1": {
          "question": "what is the Capital city of Australia ",
          "answers": {
              "a": "Melbourne",
              "b": "Sydney",
              "c": "Caneberra",
              "d": "Brisbane"
          },
          "correctAnswers": "c"
      }
  }
]`;
// no need for this to be parsed, as it already is a JS data strucure (an array in this case)
const alreadyJavaScript = [
  {
      "q1": {
          "question": "what is the Capital city of Australia ",
          "answers": {
              "a": "Melbourne",
              "b": "Sydney",
              "c": "Caneberra",
              "d": "Brisbane"
          },
          "correctAnswers": "c"
      }
  }, // In JavaScript this comma is fine, in JSON it is not!
]

// parse string
const me = JSON.parse(stringData)

// now print both to see that they are in fact identical
// Keep in mind it's an array not an object!
console.log(me[0]); 
console.log(alreadyJavaScript[0])

Note: While it is perfectly fine to put a , at the end of the last element in JavaScript, JSON does not allow that!

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda