Search code examples
arraysjsonangularsubscriptionangular5

"Unexpected token } in JSON at position 139" error message


Some problems happened when I run some angular code like this:

return this.http.get('apiurl').map(res => res as Article[]);

When I checked my cloudwatch, I found my logs look like this:

    { Items: 
[ { text: [Object],
    description: [Object],
    id: [Object],
    userid: [Object],
    title: [Object] },
    { text: [Object],
    description: [Object],
    id: [Object],
    userid: [Object],
    title: [Object] },
    { text: [Object],
    description: [Object],
    id: [Object],
    userid: [Object],
    title: [Object] } ],
    Count: 3,
    ScannedCount: 3 }

I guess this means I can fetch data from DynamoDB, but error messages showed on my page:

"Unexpected token } in JSON at position 139" enter image description here

I can get my data on codepen without any errors. I checked a lot, still could not figure out what went wrong. Could anyone help me with this problem? Any reply will be obliged

Service Code

 getArticleById(id:string){
        this.dataLoaded.next(null);
        this.dataLoadFailed.next(false);
       return this.http.get('https://example.com/'+id)
           .map(res => res as Article[]);
    }

Called service on some other page:

articles:Article[] = [];

getAllArticles(){
    this.articleService.getArticleById('all')
        .subscribe((articles:Article[])=>{
      this.articles = articles;
    })
  }

This is the data I got through codepen:

[
 {
  "title" : "fakfjdkfjdskaf",
  "description" : "fdkalfjdskalfj",
  "text" : "<p>dkalfjdsklafjdksalfjsda</p><p><br></p>",
  "userid" : "100",
  "id" : "13",
} 
, {
  "title" : "title",
  "description" : "desc",
  "text" : "aaaaa",
  "userid" : "userid",
  "id" : "100",
} 
, {
  "title" : "tiel",
  "description" : "desc",
  "text" : "text",
  "userid" : "di",
  "id" : "12",
} 
]

Solution

  • Your json string is wrong. it has extra comma(,) at last element.

    Correct json

    [
     {
      "title" : "fakfjdkfjdskaf",
      "description" : "fdkalfjdskalfj",
      "text" : "<p>dkalfjdsklafjdksalfjsda</p><p><br></p>",
      "userid" : "100",
      "id" : "13"
    },
    {
      "title" : "title",
      "description" : "desc",
      "text" : "aaaaa",
      "userid" : "userid",
      "id" : "100"
    } 
    , {
      "title" : "tiel",
      "description" : "desc",
      "text" : "text",
      "userid" : "di",
      "id" : "12"
    } 
    ]