Home > Blockchain >  How to get a JSON value using Javascript into a variable
How to get a JSON value using Javascript into a variable

Time:03-06

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?

CodePudding user response:

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

CodePudding user response:

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)

  • Related