0

I am new who is using json but I what I tried is below.

i am using this, i don't know this is true or not i found it from stackoverflow but it won't work

it is not validate my correct json format

Cœur
  • 37,241
  • 25
  • 195
  • 267
raviramani
  • 522
  • 2
  • 14
  • JSON keys need to be wrapped in doubles quotes `"` and the same goes for string values. So payload should be `{"kfl": "asdfads", "asdfklas": 123}`. For more info read https://www.json.org/ – Gabriele Petrioli Dec 28 '18 at 10:58
  • http://prntscr.com/m0jzo1 still not working, can you just check the pattern, is that true for json validation – raviramani Dec 28 '18 at 11:04
  • You're not handling numeric values with your regex (the values must be quoted as per your regex, but a number in JSON shouldn't). It would also match `{ "var": "val" "missing":"comma" }` which it shouldn't. It would also fail to understand escaped quotes. – Aaron Dec 28 '18 at 11:07
  • See https://stackoverflow.com/questions/2583472/regex-to-validate-json – Gabriele Petrioli Dec 28 '18 at 11:09
  • 1
    I don't know how Angular validators work, but I urge you if possible to craft one based on whether JSON.parse raises an error when handling your payload rather than using regex. The "complete validation" of the accepted answer of the question linked by @GabrielePetrioli won't work as far as I know, JS regex engine not being the one with the most features – Aaron Dec 28 '18 at 11:12
  • thanks for try to help me, but I found the solution, will post answer soon – raviramani Dec 28 '18 at 11:17
  • found solution something like your opinion @Aaron – raviramani Dec 28 '18 at 11:19
  • Nice, you're making a custom validator then? Looked it up a little bit, I'm looking forward to your answer :) – Aaron Dec 28 '18 at 11:20

1 Answers1

1

enter image description here

calling function on keyup and change event

enter image description here

use parser which will parse only if data is in json format and set parseError = true

otherwise it'll goto the catch section and set parseError = false

set error message based of parseError

raviramani
  • 522
  • 2
  • 14