How to verify existence of all fields in JSON & XML files using JFiles & XFiles

Hi,

Is there a function or way in JFiles & XFiles to verify that all the fields are in the files and that the spelling for each field is correct?

Some fields might not have any data in them but I need to make sure the fields are defined in the JSON/XML files.

An added bonus would be if I could also verify that the fields are in the correct order.

Regards

Johan de Klerk

Hi @Johan_de_Klerk

This tool from CapeSoft may help you. It doesn’t check if the fields are complete, but if you have the JSON/XML at hand, it can create the structure based on the file and should not left fields out.

As far as the order for the files, at least in my experience that is not as important as having the correct names (and case) defined in the schemas.

https://capesoft.com/jfilescode
https://capesoft.com/xFilesCode

PS. The tools are free for use as far as I know (thanks Bruce)

Hi,

Thanks for the response.
My system already creates the JSON/XML correctly and imports it correctly.
However sometimes the users does not supply their files in 100% the correct format and field names and then everything breaks.

That is why I am trying to get a solution to verify the JSON/XML files before loading and processing them.

Regards

Johan de Klerk

Hi Johan,

I think you will need to handle this yourself in ValidateRecord.

You can derive the class like this:

json             Class(jsonClass)
ValidateRecord     Procedure(), Long, Proc, Virtual
                 End

Then implement the method:

json.ValidateRecord Procedure ()
  Code
  ! Check required fields manually
  IF INV:CustomerID = 0 OR INV:CustomerID = ''
    Message('Missing required field: CustomerID')
    RETURN jf:filtered
  END
  IF INV:TotalAmount = 0
    Message('Missing or invalid: TotalAmount')
    RETURN jf:filtered
  END
  RETURN jf:ok

This does not automatically verify all fields—you need to check each required field manually inside ValidateRecord.

Alternatively, you could use ValidateField if you need finer control over individual fields.

Maybe Bruce might have a better answer.

Mark.

Hi Mark,

Thank for the reply and suggestion.
Will try and see if I can make it work.

Will also see if maybe Bruce have another suggestion.

Regards

Johan de Klerk

Very true. That’s usually the main problem with API calls.

Another tool I use to check the responses is Postman, but requires an extra job from my side if no WSDL is provided. What that I can see the raw responses and validate fields. But is not an automated process.

So let’s kill the easy one first. Fields in JSON and XML have no “order”. There is no such thing as correct, or incorrect, order because the fields themselves are not ordered. In other words creators, and parsers, are free to construct the structure in any order they like.

I’ll use “json” to refer to “xml and json” here…

Generally speaking the “matching” of fields in the json to your structure are not “1 to 1”. In other words the json is allowed to contain extra fields (they are ignored on input) and they are allowed to not include some values (the receiver then supplying reasonable defaults.

Bear in mind that the json is nested, and contains “lists”, so each node in the tree can contain a bunch of fields, which may in turn be a different bunch to the next sibling mode. You are suggesting that each node, through the whole file, be checked against some valid list of fields for that node, both to detect items you don’t recognise or to complain when a node is “insufficiently populated”.

both jFiles and xFiles (xFilesTree class) have a AssignMissingField method. When loading the clarion structure from the json, if a field is not found the AssignMissingField is called. You can supply a default here or generate an error.

To determine if there are “extra fields” you would need to walk the tree (using the supplied GET methods) checking each node to see if you like the name.

1 Like

Yeah, the beauty of JSON is that the structure is whatever you get and it can contain, or not contain, anything, in any order.

Makes validation interesting but if you have fields that you consider “required” then you have to verify both the existence of the field name and the data passed.

It’s up to you what you do about unexpected fields, you can either ignore them, or warn the poster that the data is not correct, similar to the way Clarion tells you that you’ve misspelled a field name when building - which is probably the best option for identifying typos.

If your system already has a way to use javascript, another way to go could be to use a schema validator library such as ajv. https://ajv.js.org/.

Hi Bruce,
Thanks, one lest thing to worry about.

Regards
Johan de Klerk

Hi Bruce,

Thanks will take a look and see how to make it work.

Regards
Johan de Klerk

Hi,

I think is a bit is a bit above my capabilities.
But I will try and take a look.

Regards
Johan de Klerk

Hi,

Thanks for the suggestion.
Will look into this.

Regards
Johan de Klerk