Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

254
Vistas
How to get a JSON value using Javascript into a variable

So I have this JSON:

{
    "success": {
        "total": 1
    },
    "contents": {
        "quotes": [
            {
                "quote": "When you win, say nothing. When you lose, say less.",
                "length": "51",
                "author": "Paul Brown",
                "tags": {
                    "0": "inspire",
                    "1": "losing",
                    "2": "running",
                    "4": "winning"
                },
                "category": "inspire",
                "language": "en",
                "date": "2022-03-05",
                "permalink": "https://theysaidso.com/quote/paul-brown-when-you-win-say-nothing-when-you-lose-say-less",
                "id": "3dlKxoNAOZsB__Nb61H95weF",
                "background": "https://theysaidso.com/img/qod/qod-inspire.jpg",
                "title": "Inspiring Quote of the day"
            }
        ]
    },
    "baseurl": "https://theysaidso.com",
    "copyright": {
        "year": 2024,
        "url": "https://theysaidso.com"
    }
}

I am trying to grab the text in the author and quote fields into two respective variables. I've already assigned the variable JSON to the json contents. What I have right now is the following below that doesn't work.

QUOTE = JSON.contents.quotes[0].quote;
AUTHOR = JSON.contents.quotes[0].author;

What am I doing wrong? And what would be the correct approach?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

did you require the json file? it works fine for me. also add let or var before the variable

var json = require('./2.json')

let QUOTE = json.contents.quotes[0].quote;
let AUTHOR = json.contents.quotes[0].author;

console.log(QUOTE, AUTHOR);

this output is When you win, say nothing. When you lose, say less. Paul Brown

about 4 years ago · Juan Pablo Isaza Denunciar

0

I've already assigned the variable JSON to the json contents.

From your explanation, I assume that you did assign the contents object to the variable JSON. In that case, you needed to access the properties using JSON.quotes[0].quote and JSON.quotes[0].author. Please refer the snippet below.

var yourObject = {
  "success": {
    "total": 1
  },
  "contents": {
    "quotes": [{
      "quote": "When you win, say nothing. When you lose, say less.",
      "length": "51",
      "author": "Paul Brown",
      "tags": {
        "0": "inspire",
        "1": "losing",
        "2": "running",
        "4": "winning"
      },
      "category": "inspire",
      "language": "en",
      "date": "2022-03-05",
      "permalink": "https://theysaidso.com/quote/paul-brown-when-you-win-say-nothing-when-you-lose-say-less",
      "id": "3dlKxoNAOZsB__Nb61H95weF",
      "background": "https://theysaidso.com/img/qod/qod-inspire.jpg",
      "title": "Inspiring Quote of the day"
    }]
  },
  "baseurl": "https://theysaidso.com",
  "copyright": {
    "year": 2024,
    "url": "https://theysaidso.com"
  }
}

let JSON = yourObject.contents

let yourQuote = JSON.quotes[0].quote
let yourAuthor = JSON.quotes[0].author

console.log(yourQuote)
console.log(yourAuthor)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda