1

I am trying to validate a json object below

[{"post_content":"<div class=table-responsive><table class=table><tr><td></td></tr></table></div>"}, {"post_content":"<div class=table-responsive><table class=table><tr><td>
</td></tr></table></div>"}]

It fails. I have made it on one line like below

[{"post_content":"<div class=table-responsive><table class=table><tr><td></td></tr></table></div>"}, {"post_content":"<div class=table-responsive><table class=table><tr><td></td></tr></table></div>"}]

Now it works. I have more objects that cannot be edited repeatedly like this. Is there any workaround?

2 Answers2

2

According to the RFC-4627

These are the six structural characters:

  begin-array     = ws %x5B ws  ; [ left square bracket

  begin-object    = ws %x7B ws  ; { left curly bracket

  end-array       = ws %x5D ws  ; ] right square bracket

  end-object      = ws %x7D ws  ; } right curly bracket

  name-separator  = ws %x3A ws  ; : colon

  value-separator = ws %x2C ws  ; , comma

Insignificant whitespace is allowed before or after any of the six structural characters.

  ws = *(
            %x20 /              ; Space
            %x09 /              ; Horizontal tab
            %x0A /              ; Line feed or New line
            %x0D                ; Carriage return
        )

In your case 1 there is a whitespace next to a <td> which is why the json stands invalid.

In your case 2 the same whitespace is gone and hence you have a valid json.

Community
  • 1
  • 1
Abdullah Khan
  • 12,010
  • 6
  • 65
  • 78
0

The problem characters can be escaped or removed :

var json = `[{"post_content":"<div class=table-responsive><table class=table><tr><td></td></tr></table></div>"}, {"post_content":"<div class=table-responsive><table class=table><tr><td>
</td></tr></table></div>"}]`

console.log( JSON.parse( json.replace(/\n/g, '\\n') ) )
Slai
  • 22,144
  • 5
  • 45
  • 53