1

I am trying to get data from a JSON file into an angular app, the thing is I will be using this file as a local database and it will contain more than one array so I have to name the array but when I try to define it the vs points it out as an error.

Here's the array

var historybooks = [
{
    "name" : "book1",
    "id" : "his-1",
    "author" : "omar",
    "discription" : "lorem ipsum dolor ....",
    "image" : "./assets/books-images/book1.png"
}
]

vs error "Expected a JSON object, array or literal."

I also tried with let instead of var and get the same error.

ibenjelloun
  • 7,425
  • 2
  • 29
  • 53
Omar Abdelhady
  • 1,528
  • 4
  • 19
  • 31

5 Answers5

1

Use the below code if the file is a JS file:-

export var historybooks = [
{
    "name" : "book1",
    "id" : "his-1",
    "author" : "omar",
    "discription" : "lorem ipsum dolor ....",
    "image" : "./assets/books-images/book1.png"
}
]

If you want to name the file as .json, then use the below code:-

[
{
    "name" : "book1",
    "id" : "his-1",
    "author" : "omar",
    "discription" : "lorem ipsum dolor ....",
    "image" : "./assets/books-images/book1.png"
}
]

You can make an Ajax request to fetch the .json file.

Ankit Sharma
  • 1,674
  • 8
  • 11
  • second one it would be only one array i want to add more than one ,, is it possible ? , cause i want to use as a DB – Omar Abdelhady Jul 06 '18 at 13:04
  • If you are making it a .json file, you can make an object and then define different arrays in it – Ankit Sharma Jul 06 '18 at 13:06
  • Nice alternative for including JS files, but just keep in mind that [**export**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export#Browser_compatibility) is not supported in older browsers such as Internet Explorer. – cнŝdk Jul 06 '18 at 13:19
  • Thanks for the clarification @chŝdk but would it not work since it is an angular app and would be compiled into native JS code using webpack? – Ankit Sharma Jul 06 '18 at 14:55
1

In fact you can't write declarations, inside a JSON file all that you can do is write your JSON object or array directly on the file.

That's why you are getting the error:

"Expected a JSON object, array or literal."

Because you are writing JavaScript code inside of your JSON file, remove the declaration part:

[
{
    "name" : "book1",
    "id" : "his-1",
    "author" : "omar",
    "discription" : "lorem ipsum dolor ....",
    "image" : "./assets/books-images/book1.png"
}
]
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
0

It could be that the code after this is being interpreted as an index into the array or something like that
Try adding a semi-colon after the array :

var historybooks = [
  {
      "name" : "book1",
      "id" : "his-1",
      "author" : "omar",
      "discription" : "lorem ipsum dolor ....",
      "image" : "./assets/books-images/book1.png"
  }
]; // <= here
imma
  • 4,912
  • 2
  • 17
  • 10
0

Just remove decalartion part of your json , and your good to go something like this.

   [
    {
    "Neymary": "pleaseStopDiving"
    }
   ]
The Dead Man
  • 6,258
  • 28
  • 111
  • 193
0

You can't declare a variable inside a JSON.Remove the declaration.

If you want to declare more array's you can do this in the same file:

{
"array1":[{"key":"value","key":"value"}],
"array2":[{"key":"value","key":"value"}]
}
anud33p
  • 198
  • 4
  • 15