Validate JSON schema for expected values - json

I have a JSON which I would like to validate.
There are is an object inside an array, within each object there is a property called name.
I want 1st validate that there are 3 objects.
And I want to validate the value of each of the property.
{
"hello": [
{
"world": "value 1"
},
{
"world": "value 2"
},
{
"world": "value 3"
}
]
}
I want to validate that the JSON has value 1, value 2, value 3 using a JS0N schema

Using the language of JSON Extended Structural Schemas (JESS), the three requirements could be written in JSON as follows (assuming that you meant world rather than name):
["&",
{ "hello": [ {"world": "string"} ] },
{"forall": ".[hello]|length", "equal": 3 },
{"setof": ".[hello][]|.[world]", "supersetof": ["value 1", "value 2", "value 3" ]}
]
This may not be exactly what you want, e.g. perhaps you want the constraints to be written without reference to the name of the top-level key. This could be accomplished as follows:
["&",
{"forall": ".[]", "schema": [ {"world": "string"} ] },
{"forall": ".[]|length", "equal": 3 },
{"setof": ".[][]|.[world]", "supersetof": ["value 1", "value 2", "value 3" ]}
]
Also you could modify the above to express the requirements without preventing the objects from having additional keys. It all depends on what you really want.
Note that the JESS checker requires jq to run. There is a ruby gem for jq.

Related

JSON structure quesiton: multiple and different JSON entries, one txt file

I am trying to do some work with log visualization tools (Elastic and/or Splunk), but first I need to produce and format the log files from a simulation I am writing. My question, which I can't seem to find clear guidance on is:
How to store multiple, what I believe are root element JSON entries in a single text file
How to work with nested JSON structures
I am ultimately trying to have every entry follow the same form:
{"entry_id": 1,
"TIME": "12:00:12Z012/01/2022",
"LOG_TYPE":"ERROR_REPORT",
"DATA": {
"FIELD A" : "ABC",
"FIELD B" : "DEF"
}
},
{"entry_id": 2,
"TIME": "12:15:12Z012/01/2022",
"LOG_TYPE":"STATUS_REPORT",
"DATA": {
"FIELD C" : "HIJ",
"FIELD D" : 123
}
}
Some options I saw
Use an array []
Use NDJSON
Use some log template??
Any insight would be helpful
JSON files need to be a single object and can't be INVALID themselves.
Option 1: Create a single file for each of the objects, using a numeric naming system for the files, then iterating over the files in your method.
Option 2: Create a single file but have each entry contained in an array eg:
{
"entries": [
{
"entry_id": 1,
"TIME": "12:00:12Z012/01/2022",
"LOG_TYPE": "ERROR_REPORT",
"DATA": {
"FIELD A": "ABC",
"FIELD B": "DEF"
}
},
{
"entry_id": 2,
"TIME": "12:15:12Z012/01/2022",
"LOG_TYPE": "STATUS_REPORT",
"DATA": {
"FIELD C": "HIJ",
"FIELD D": 123
}
}
]
}

NiFi EvaluateJSONPath loop through array to get correct value

here is an example of some JSON that I need to deal with:
{
"name": "John Smith",
"active": "yes",
"cpair": [
{
"title": "ADDRESS",
"charVal": "1234 Fulcrum lane"
},
{
"title": "phone",
"charVal": "555-7600"
}
]
}
So I'm using the evaluateJsonPath processor to add these values as attributes in my flowfile. thats easy for some. I can just set name equal to $.name and active to $.active. But lets say I need to give the attribute 'address' the value of "1234 Fulcrum lane". How do I assign that attribute the proper charVal value that matches up with the correct title?
according to Jayway JsonPath documentation
this should work:
$.cpair[?(#.title == 'ADDRESS')].charVal

JSON Path - size of root array element (NeoLoad)

I'm using NeoLoad 6.3.1 at the moment and am trying to get the length of an array where the array itself is the root element.
Given the following sample JSON:
[
{ "id": 1, "title": "Item 1" },
{ "id": 2, "title": "Item 2" },
{ "id": 3, "title": "Item 3" },
{ "id": 4, "title": "Item 4" },
{ "id": 5, "title": "Item 5" }
]
I want to just get back the answer of "5".
If I use the JSON Path Online Evaluator, I can use $.length and it returns:
[ 5 ]
In NeoLoad 6.3.1, that returns an error.
As NeoLoad is Java-based, I am assuming that they're using the com.jayway.jsonpath's json-path library (or something similar). Based on the documentation there I updated the query to be $.length() but did not have any luck.
Any suggestions?
In Neoload, there is "Variable Extractor" action where you can provide left boundary, right boundary for any one of the subnode in your array. e.g.
LB:"title": "
RB: " }
and select "extract call occurrences" option. This variables can be accessed via "variablename_matchNr" which gives count of all occurrences of given extraction.
Better explained here:
http://answers.neotys.com/questions/590268-created-variable-extractor-last-occurrence-extracted-values
Neload also provides JSON path expression in variable extractor where user can select any one node and select "extract call occurrences".

How do i document optional RESTful JSON API attributes?

I've been trying to figure out how to design the documentation for an API I'm building. I've been using Swagger (swagger.io) along with JSONSchema to help me structure the documentation, but I've run into a snag. Some of our object will have lots of what we call "metadata" attached to them, which is basically an arbitrary and variable dictionary of key/values. For example:
{
"id": "aabbbccdd",
"name": "Object 1",
"metadata": {
"attributeA": "This is a text attribute",
"attributeB": {
"key1": "complex attribute",
"key2": "complex attribute 2",
},
"attributeC": 1234
}
},
{
"id": "eeffffggghh",
"name": "Object 2",
"metadata": {
"attributeA": "This is a text attribute with a different value",
"attributeD": "Another text attribute",
"attributeE": True
}
}
So I could document the object representation by enumerating all the metadata attributes, but the list is long and will likely vary over time.
Is there another way to approach documenting this, or designing the API in a different way?

Json Data Decoding without Library

I don't want to use a library. I want to figure out how to parse JSON data properly myself.
Example content :
If I was to parse this :
{"Name": [
{
"Type": "Type1",
"Content": "Content 1"
},
{
"Type": "Type1",
"Content": "Content 2"
},
{
"Type": "Type2",
"Content": "Content 3"
},
{
"Type": "Type2",
"Content": "Content 4"
}
]
}
Would I simply go about using indices and substrings and so on?
Or is there something about String manipulation that I am missing out on?
In javascript, eval() evaluates the expression. JSON is just a JS expression so it evaluates to an object. This is assuming the input is a valid JSON string. eval() runs all types of javascript code so beware of security.