I am having a shell script . in my shell script i have a command that returns the following data as a json .
{
"remoteIds": [
{
"remoteId": "[fd00:10:244:1:0:0:0:12]--(http://fd00-10-244-1--12.nspace.pod:8080)",
"requestsReceived": 1
}
}
i want to validate the above output with a json template. in my json template i can provide the port, nspace , protocol (http) & requestsReceived . How can i design a json template and validate against the json data . i can use jq and bash script
The following will be a template in my mind
{
"remoteIds": [
{
"remoteId": "[*]--(http://*.nspace.pod:8080)",
"requestsReceived": 1
}
}
thank you
2
Answers
If you don’t want to tangle with a full-fledged generic schema definition ecosystem and don’t mind "rolling your own" validation engine, you might wish to consider the following illustration:
This incidentally works with all three of the major jq implementations:
the standard C-based implementation; gojq; and jaq.
Here’s how you could use the JESS validation tool for JSON.
(JESS stands for JSON Extended Structural Schemas, and refers to
both a schema specification language and a validation tool. The
home page on github is at https://github.com/pkoppstein/JESS)
You can either create a JESS schema by hand, or
generate a basic basic structural schema as described below, and then
tweak it according to your requirements. Either way, let’s
assume your structural schema is in a file named
validate.schema and that is looks like this:
Feel free to tweak the above regexp, and indeed the entire schema, in accordance with your requirements.
Assuming you have installed the JESS validation tool, you would run it like so:
Note that the JESS script only reports non-conformances.
That is, it produces no output if the JSON conforms to the schema.
To create a basic structural schema automatically, you can use "schema.jq"
(https://gist.github.com/pkoppstein/a5abb4ebef3b0f72a6ed) like so: