-1

how to validate if the text input is JSON with angularjs or js? I just found a way to do this with angular 2

bummi
  • 27,123
  • 14
  • 62
  • 101
taskiukas
  • 83
  • 1
  • 10
  • 1
    how about JSON.parse ? – Chen Kinnrot Sep 17 '17 at 12:03
  • I think you can find your answer in here [how-to-check-if-a-string-is-a-valid-json-string-in-javascript-without-using-try](https://stackoverflow.com/questions/3710204/how-to-check-if-a-string-is-a-valid-json-string-in-javascript-without-using-try) – ganjim Sep 17 '17 at 12:05
  • here is another method using JSON.parse [how to test if a string is json or not](https://stackoverflow.com/questions/9804777/how-to-test-if-a-string-is-json-or-not) – ganjim Sep 17 '17 at 12:07
  • 1
    Please do not vandalize your posts. By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the [CC BY-SA 3.0 license](https://creativecommons.org/licenses/by-sa/3.0/)). By SE policy, any vandalism will be reverted. If you would like to disassociate this post from your account, see [What is the proper route for a disassociation request?](https://meta.stackoverflow.com/q/323395) – Suraj Rao Sep 20 '17 at 09:35

1 Answers1

0

Here's the Code for the Same:

let checkForJSON = (str) => {
    try {
       JSON.parse(str)
    } catch (err) {
        return false
    }
    return true
}
Daksh M.
  • 4,589
  • 4
  • 30
  • 46