-2
  • I was getting this error in the console.

core.js:6014 ERROR SyntaxError: Unexpected number in JSON at position 2
    at JSON.parse (<anonymous>)
    at SafeSubscriber.httpObj.userService.getTaxRate.subscribe.httpObj [as _next] (geotax.component.ts:404)
    at SafeSubscriber.__tryOrUnsub (Subscriber.js:183)
    at SafeSubscriber.next (Subscriber.js:122)
    at Subscriber._next (Subscriber.js:72)
    at Subscriber.next (Subscriber.js:49)
    at MapSubscriber._next (map.js:35)
    at MapSubscriber.next (Subscriber.js:49)
    at FilterSubscriber._next (filter.js:33)
    at FilterSubscriber.next (Subscriber.js:49)

  • When i debug the code i found out that this was coming because the sevice which was returning a latitude, was appending 0 at the start . Example latitude = "-048.55" instead of latitude="-48.55".
  • How can i remove this '0' at the start of this latitude?
sunny
  • 51
  • 12
  • 4
    You should fix the service so it returns valid JSON instead of trying to hack invalid JSON after it arrives on the client. – Quentin Apr 12 '20 at 11:05
  • This is specific to some cases as sometimes valid json is coming – sunny Apr 12 '20 at 11:07
  • I assume the cases it is specific to are ones where the latitude is between -100 and +100 (not inclusive). You should still fix the code generating the invalid JSON and not try to hack it once it gets to the client. – Quentin Apr 12 '20 at 11:08
  • maybe a number type would help better for stringify than a string. – Nina Scholz Apr 12 '20 at 11:09
  • 1
    The JSON endpoint should be fixed: it's not something you can do much on your end. That's because JSON cannot support leading zeros in numbers, as leading zeros indicate an octal number: https://stackoverflow.com/questions/27361565/why-is-json-invalid-if-an-integer-begins-with-a-leading-zero. Since octal numbers cannot contain '8', then `-048.55` will throw an error as `JSON.parse` fails to parse an invalid octet. – Terry Apr 12 '20 at 11:11

1 Answers1

1

You can clean the data before processing using helper function like below -

let coordinates = [{
    latitude: '56.56'
  },
  {
    latitude: '-048.55'
  }
];

let cleanData = (data = []) => {
  return data.map(item => {
      return {
        ...item,
        latitude: Number(item.latitude).toString()
      }
  })
}

console.log(cleanData(coordinates));
Varun Goel
  • 435
  • 2
  • 7
  • 1
    It is not that simple. OP gets the data as an invalid JSON *string* which you cannot just parse with `JSON.parse`. You would have to fix the string first. – str Apr 12 '20 at 11:21
  • In that case a reviver function may help.We can transform the data before it is returned. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse – Varun Goel Apr 12 '20 at 11:41
  • Even using a reviver function, you cannot parse invalid JSON. – str Apr 12 '20 at 12:09
  • Fixing the inccorect JSON response from source is the best choice left rathar adding any patches then. – Varun Goel Apr 12 '20 at 15:47