0

I've got some JSON that looks like this:

[
     {
         _id: ObjectId("544809736654daf1ea897ca"),
         project: "demo",
         tools: ['ajax', 'javascript', 'html'],
     },
     {
         _id: ObjectId("322148965654daf1ea81ca"),
         project: "trial",
         tools: ['haskell'],
     }
]

I've saved it in a file called items.

I'm trying to import it to my Angular project with this code:

app.service("getItemsService", 
    function($http, $q){
        return {
            getItems: function getItems(){
                return $http.get('data/_items').success(function(data){
                    return data;
                }); 
            }
        }
    }
);

but when I do, I get an error saying:

SyntaxError: Unexpected token _
    at Object.parse (native)

I've tried everything I can think of to fix this - i.e. I've named the file items.json, I changed _id to id, I've tried adding {'Content-Type': 'application/json'} to get() function as a parameter to indicate its JSON. Nothing is working! Any tips?

2
  • what does the server side look like? 'data/_items' as the get method looks a little goofy to me. What are you doing with the returned data, where is the error happening? Commented Nov 13, 2014 at 15:53
  • Is your issue fixed ? Are you able to accept one of the two proposed answer ? Commented Nov 20, 2014 at 9:47

2 Answers 2

3

Your angular service seems good. It is your "JSON" content file that is not formated as a regular JSON string:

  1. properties as to be strings double quoted
  2. values as to be strings double quoted or object {} or arrays []
  3. functions couldn't be used in json strings (your ObjectId() function)
  4. Last elements in arrays can't finish by a comma as the previous elements

Try this instead :

[
     {
         "_id": "544809736654daf1ea897ca",
         "project": "demo",
         "tools": ["ajax", "javascript", "html"]
     },
     {
         "_id": "322148965654daf1ea81ca",
         "project": "trial",
         "tools": ["haskell"]
     }
]
Sign up to request clarification or add additional context in comments.

1 Comment

You can use this online json parser tool to check your (small) JSON strings or JSON content files.
1

Always try your JSON in a linter in that kind of problem like http://jsonlint.com/ It would have tell you

Parse error on line 2:
[    {        _id: ObjectId("54480
--------------^
Expecting 'STRING', '}'

That is a little less cryptic : it tells you that _id is not a String => you'r missing the "

After that it will tell you that :

Parse error on line 3:
...   {        "_id": ObjectId("5448097366
----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['

ObjectId is not valid for JSON, you have to find somehting else, like only the id, or all in a String :

[
{
    "_id": "544809736654daf1ea897ca",
    "project": "demo",
    "tools": [
        'ajax',
        'javascript',
        'html'
    ],

},
{
    "_id": "322148965654daf1ea81ca",
    "project": "trial",
    "tools": [
        'haskell'
    ],

}
]

But hey, it's not finish :

Parse error on line 5:
...ols": [            'ajax',            
----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', ']'

Well yeah, ' is not valid, some parser actualy fail on single quote.

[
{
    "_id": "544809736654daf1ea897ca",
    "project": "demo",
    "tools": [
        "ajax",
        "javascript",
        "html"
    ],

},
{
    "_id": "322148965654daf1ea81ca",
    "project": "trial",
    "tools": [
        "haskell"
    ],

}
]

Still not there :

Parse error on line 9:
...    ],            },    {        "_i
---------------------^
Expecting 'STRING'

it seems that there is a trouble around line 9, and if you look closely you will see a , with nothing behind, let's remove the useless ,

[
{
    "_id": "544809736654daf1ea897ca",
    "project": "demo",
    "tools": [
        "ajax",
        "javascript",
        "html"
    ]
},
{
    "_id": "322148965654daf1ea81ca",
    "project": "trial",
    "tools": [
        "haskell"
    ]
}
]

And here it is !

enter image description here

2 Comments

Wow. Ok, that's interesting. So is there any way to import JSON as a Javascript object?
JSON IS a javascript object, but it has to be valid ;)

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.