0

I have a JSON file I use to store quotes for some Quote Generator I'm building. Recently I got this error in my terminal (see screenshot below).

Expected a JSON object, array or literal.json

This is what my JSON looks like

data = [
{
    "number": "1",
    "author": "Von R. Glitschka",
    "quote": "The client may be king, but he's not the art director."
},
{
    "number": "2",
    "author": "Frank Capra",
    "quote": "A hunch is creativity trying to tell you something."
},
{
    "number": "3",
    "author": "Steven Heller",
    "quote": "As a profession, graphic designers have been shamefully remiss or ineffective about plying their craft for social or political betterment."
}]

I've tried all I could. but the error keeps coming, what could be wrong?

enter image description here

thlpswm
  • 45
  • 3
Bolaji Ayodeji
  • 37
  • 1
  • 3
  • 8

6 Answers6

11

You just need to format your file a bit like this:

{
  "data" : [
    {
        "number": "1",
        "author": "Von R. Glitschka",
        "quote": "The client may be king, but he's not the art director."
    },
    {
        "number": "2",
        "author": "Frank Capra",
        "quote": "A hunch is creativity trying to tell you something."
    },
    {
        "number": "3",
        "author": "Steven Heller",
        "quote": "As a profession, graphic designers have been shamefully remiss or ineffective about plying their craft for social or political betterment."
    }]
}

And save this with a .json extension.

barbsan
  • 3,418
  • 11
  • 21
  • 28
DaminiVyas
  • 308
  • 3
  • 15
0

I had a similar problem but finally figured it out like this: firstly, you need to make sure you have an eslintrc.json file, and then inside that json file you add this json object:

{
  "extends": [
    "plugin:react/recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:prettier/recommended"
  ]
}

which also works if you've a typescript or prettier errors.

Siddhartha Mukherjee
  • 2,703
  • 2
  • 24
  • 29
0

The error occurred for me but mine one just gone by changing file extension form json to js.

-2

It looks like, given the data you have and the code you are using to get a random number, your number is often exceeding the number of objects you have in your array.

For example,

Math.floor(Math.random() * 50)

Could end up setting random to 13, which greatly exceeds the number of values in your array.

If you would like to get a random number between 0 and 2, you can use:

random = Math.floor(Math.random() * Math.floor(3));
Sara Chipps
  • 9,322
  • 11
  • 58
  • 103
-3

An alternate approach to this is

data = [
{
"number": "1",
"author": "Von R. Glitschka",
"quote": "The client may be king, but he's not the art director."
},
{
"number": "2",
"author": "Frank Capra",
"quote": "A hunch is creativity trying to tell you something."
},
{
"number": "3",
"author": "Steven Heller",
"quote": "As a profession, graphic designers have been shamefully remiss or ineffective about plying their craft for social or political betterment."
}];

console.log(data);
var random = Math.floor(Math.random() * data.length); 
console.log(data[random].quote);
console.log(data[random].author);
proton
  • 1,639
  • 6
  • 18
  • 30
  • Because the length of the array is dynamic it makes sense to use `data.length` as the limit for generating a random number so in the event it changes you are certain it'll never be out of range. – proton Dec 17 '18 at 22:27
  • Actually, my code still works and I tried your's it worked too. But the error is pooping in my JSON and I don't know why? – Bolaji Ayodeji Dec 17 '18 at 22:30
  • Thanks for the `data.length` reminder. I was actually updating the data length manually but it makes more sense to make it dynamic as you rightly said. – Bolaji Ayodeji Dec 17 '18 at 22:31
-4

Well first, this isn't exactly a JSON format. This is an array of objects. Your JSON can not have a variable assignment like the one you have var data = ..... Depending on why you are getting the error or what you intend to do with the data. You have 2 options:

  1. Convert this array into an acceptable JSON object, like so: $ JSON.stringify(data).

  2. You can work with this data directly as an array that it is, just by storing it either as a js variable or in a js file. Then you can easily manipulate it like an array.

Ola John Ajiboye
  • 123
  • 1
  • 1
  • 9