Jump to content
Mark-

JSON text validation...

Recommended Posts

Hello,

 

I know there are several online JSON validation sites and I am seeking one or both of below.

 

1.  Delphi code or DLL that can validate JSON text.

2.  A website I can POST the text to validate and get the results.

 

Any ideas?

 

Thanks,

 

Mark

 

 

Edited by Mark-

Share this post


Link to post

If you use System.JSON and parse with exceptions enabled, it'll tell you where an error is. However, it will only show the first error and not all, but at least it's a built-in option.

Share this post


Link to post
11 minutes ago, aehimself said:

If you use System.JSON and parse with exceptions enabled, it'll tell you where an error is. However, it will only show the first error and not all, but at least it's a built-in option.

Thanks

Share this post


Link to post
17 minutes ago, programmerdelphi2k said:

JSONBuddy excellent tools offline!!!

Thanks

Share this post


Link to post

As it turns out , manually you can use TJSONValue.ParseValue and TJSONByteReader just like TJSONValue.ParseJSONValue does. But instead of throwing an exception, just mark the wrong items, using TJSONByteReader.OffsetToPos to get the line / character position from the offset.

When an error happens, just skip 1 byte in TJSONByteReader and repeat.

 

In theory, this could work.

Share this post


Link to post
Quote

{"foo": "bar, quz deg}

testing many online-Validator (to avoid myself intervention), Each one points in one direction:

  • some points an error at position 22 -> "}"
  • others, points out the error in the first quote: "bar
  • others simply say: JSON invalid
  • the more experts fix the JSON by adding "escapes" and accept the new JSON! of course, causing a feeling of "all right, by processing", which can lead to a bigger error, further down the line!

So we can see that it's a matter of perspective who is in control and who is not!


In general, if you don't have a tool with expertise, a simple "Exception" would already be indicated to prevent the procedure from proceeding, or even the task as a whole!

Share this post


Link to post
5 hours ago, Fr0sT.Brutal said:

Okay how will you catch all errors here:

{
 "foo": "bar,
 quz deg
} 

when parser will stop after ","?

I thought testing would be line based, but what do I know. The one site I toyed with the most is https://jsononline.net/json-validator.

For the above example results:

"Error: Parse error on line 2: { "foo": "bar, quz deg} ---------^ Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined' "

Share this post


Link to post
2 hours ago, Mark- said:

I thought testing would be line based

JSON has no restrictions regarding lines, it could even be one-liner. However, with custom or somewhat controllable parser you'll be able to skip invalid lines unless they influence following lines

{

"foo": [ invalid,

"valid": "ok"

]

}

Edited by Fr0sT.Brutal

Share this post


Link to post
22 minutes ago, Fr0sT.Brutal said:

JSON has no restrictions regarding lines,

Right, I should have said "block" or "terminator" based.

Share this post


Link to post
On 12/14/2022 at 12:57 PM, aehimself said:

If you use System.JSON and parse with exceptions enabled, it'll tell you where an error is. However, it will only show the first error and not all, but at least it's a built-in option.

I found TJSONObject.ParseJSONValue and tried it. With invalid JSON text it just returns a nil result as expected, no exception is generated, with Delphi 10.2.

There are no exceptions set to ignore in "language exceptions" that are related to JSON.

 

Any ideas?

Share this post


Link to post
45 minutes ago, programmerdelphi2k said:

  TJSONObject.ParseJSONValue('JSON text', true,  true);

There is not an overloaded version that accepts two boolean values in 10.2.

I do see the online help does list one for 11.2 I wonder which version it was added.

Edited by Mark-

Share this post


Link to post

I suspect you'll have to modify some existing parser to achieve the goal. You can even write your own - JSON syntax isn't rocket science and you only need passthrough checker (no document structure).

Share this post


Link to post

I only have 10.4.2 so I don't know if this version is available on 10.2, but you can try with

 

var
 jv: TJSONValue;
 tb: TBytes;
begin
 tb := TEncoding.UTF8.GetBytes(Edit1.Text);

 Try
     jv := TJSONObject.ParseJSONValue(tb, 0, [TJSONObject.TJSONParseOption.IsUTF8, TJSONObject.TJSONParseOption.RaiseExc]);
     jv.Free;
 Except
   On E:EJSONParseException Do
   Begin
     ShowMessage('JSON can not be parsed at path ' + E.Path + ', line ' + E.Line.ToString + ', position ' + E.Position.ToString);
   End
   Else
     Raise;
 End;


 

Share this post


Link to post
5 hours ago, aehimself said:

I only have 10.4.2 so I don't know if this version is available on 10.2...

Thanks, it is not. I have 10.4 but use 10.2. I think I have 10.4 installed on a VM somewhere.

Share this post


Link to post
5 hours ago, Fr0sT.Brutal said:

I suspect you'll have to modify some existing parser to achieve the goal. You can even write your own - JSON syntax isn't rocket science and you only need passthrough checker (no document structure).

Thanks

Share this post


Link to post
42 minutes ago, Mark- said:

Thanks, it is not. I have 10.4 but use 10.2. I think I have 10.4 installed on a VM somewhere.

What overloaded versions you have? Any with TJSONParseOptions will do.

Edited by aehimself

Share this post


Link to post
7 minutes ago, aehimself said:

What overloaded versions you have? Any with TJSONParseOptions will do.

There are only two options in TJSONParseOptions,

IsUTF8

UseBool

 

I found my 10.4 VM and will try to use the 10 JSON files with 10.2,

Share this post


Link to post
1 hour ago, Mark- said:

Thanks, it is not. I have 10.4 but use 10.2. I think I have 10.4 installed on a VM somewhere.

The 10.4 JSON parser uses some newer methods in other units and messing with that is not appealing.

  • Like 1

Share this post


Link to post

Sorry about it, then. I really thought these methods are already in 10.2.

 

You can check where an exception is raised in System.JSON in that earlier version and then simply backtrack to see what conditions you need to meet.

 

Unfortunately I don't have a 10.2 installation anymore to check.

Share this post


Link to post
Just now, aehimself said:

Sorry about it, then. I really thought these methods are already in 10.2.

No problem. Looking at the 10.2 code, it just exits on error with the result as nil.

I wonder if I could/should write a console app in 10.4 and call it from the 10.2 created app.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×