-2

How can I validate a JSON string at server side? Suppose I have a json coming in from UI. For example:

{
    "Name": "shivaji",
    "CoverageAmount": "1000$"
}

If someone edited it on the browser using Firebug, how can I validate it on the server side?

  • What do you want validate? The JSON as a whole or the values in it? – aksappy Jun 26 '15 at 06:44
  • Validate for what? You'd write a rule that checks the correctness. Without knowing what you require from the data, it's impossible to tell how to do it. – JJJ Jun 26 '15 at 06:44
  • 2
    All the above, and you should also add what language/environment you are developing with and what you have already tried. – vzsg Jun 26 '15 at 06:53
  • You could try to add a checksum (just the idea, not the implementation - http://stackoverflow.com/questions/811195/fast-open-source-checksum-for-small-strings) before sending the object and re-check it server-side (of course, if one would tamper with your object, one could also re-generate the checksum as well, but at least it's something) – Alex Tartan Jun 26 '15 at 07:09
  • Some one tamper the data while transforming from UI to Server in spring – Chatrapathi Shivaji Jun 26 '15 at 09:58

3 Answers3

1

Please refer following link

How to make sure that string is Valid JSON using JSON.NET

This should answer your question

Community
  • 1
  • 1
Chander .k
  • 541
  • 4
  • 12
0

Just use ObjectMaper, and catch an exception. If it throws an exception, than your Json is not valid.

This is the simplest way, hardly any regexp can give yoy this.

Beri
  • 11,470
  • 4
  • 35
  • 57
0

It depends, what server side language are you using?

If you're using javascript and node you process the request and parse with JSON.parse just as you would in the browser and then access the properties and do your validation from there.

You might be using php, get the POST body and convert json to assoc array:

$requestBody = file_get_contents('php://input');
$data = json_decode($requestBody, true);