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.

1
  • u can't "name" an array in json file. what you cn do is add a property to object as an identifier Commented Jul 6, 2018 at 13:00

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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
If you are making it a .json file, you can make an object and then define different arrays in it
Nice alternative for including JS files, but just keep in mind that export is not supported in older browsers such as Internet Explorer.
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?
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"
}
]

3 Comments

so if i want more than one array i need to make more than one JSON file
@OmarMohmedAbdelhady Yes, you need to make each array in a separate file, you can't use var historybooks = declarations.
@OmarMohmedAbdelhady Great, glad it helps.
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"}]
}

Comments

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

1 Comment

no i had to make it in separate file or an object of arrays
0

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

   [
    {
    "Neymary": "pleaseStopDiving"
    }
   ]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.