-1

I have written a method in an Angular 2 app that creates and returns an Observable of a class, after making a server call that returns JSON. However, the JSON is poorly formatted, so I need a quick-fix to edit the returned JSON before sending it on to the .json() method, which currently throws an error due to the poorly-formatted JSON.

Specifically, I need to find an errant comma in the JSON that appears after the closing bracket of an array, and remove it.

My Question

What exactly should that Observable.map function look like? How do I find and manipulate the actual JSON string, and then send it along? I'm new to all of this, so details would be appreciated.

getList(): Observable<Thing[]> {
  let response = this.http.get('/api/get-things');
  let fixed = response.map(____WHAT GOES HERE?_____);
  return fixed.map((r: Response) =>r.json as Thing[]);
}
Jerry Huckins
  • 498
  • 3
  • 7
  • 22
  • 2
    It's not clear what you mean, but it seems like another map on the observable would suffice. Either before calling the JSON method if you need to modify the response body or after if you need to modify the resulting object. – jonrsharpe Feb 08 '17 at 19:32
  • Thank you for the feedback. I edited the question to add more details and describe more specifically what I want. – Jerry Huckins Feb 08 '17 at 19:41
  • What have you tried putting there? Have you tried using a debugger or just the console to see what you're getting in? Read the docs on the response object? Please give a [mcve] of the specific problem. – jonrsharpe Feb 08 '17 at 19:42
  • I did read docs on Response yesterday. I think I asked the wrong question. Maybe I should have asked "How do I alter JSON data stored in a Response?" I console.logged the Response and I can see the JSON in the `_body` property. But I can't figure out how to manipulate it. – Jerry Huckins Feb 08 '17 at 20:15
  • Then just pull the `text` instead of the `json` and `JSON.parse` it yourself after whatever modification: http://stackoverflow.com/a/41418145/3001761 – jonrsharpe Feb 08 '17 at 20:16
  • Ok, how did you know there was a `text` function on a Response? This is [my reference](https://angular.io/docs/ts/latest/api/http/index/Response-class.html) from the angular.io site, and it says nothing about `text`. I did successfully get the raw JSON using `text`, which is great thanks. Which function do I use to save my changes back to the Response? – Jerry Huckins Feb 08 '17 at 20:27
  • It `extends Body`, where you can see the method: https://github.com/angular/angular/blob/2.4.6/modules/%40angular/http/src/body.ts#L38. Why do you need to save the changes back to the response at all? Just pass the parsed JS objects on to the next step. – jonrsharpe Feb 08 '17 at 20:29

1 Answers1

2

Assuming you have a fix method:

const fix = (json: string) => {
  // ...
}

You could do something like this:

const things: Observable<Thing[]> = this.http.get('/api/get-things')
  .map(response => response.text())
  .map(fix)
  .map(fixedJson => JSON.parse(fixedJson) as Thing[])

However it's a very bad idea. Fix the server side code!

Balázs Édes
  • 13,452
  • 6
  • 54
  • 89