JSON Path - size of root array element (NeoLoad) - json

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".

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
}
}
]
}

JSON server and custom routes

I'm using JSON server. If I initialize it with npx json-server --watch database.json --port 5000 and then open http://localhost:5000/items I can see all the items. If I go to http://localhost:5000/items/1 I have only one item. My question is - how can I change single item route so I get the first item by entering http://localhost:5000/items/myCustomId1? In other words, how can I replace id with customId?
{
"items": [
{
"id": 1,
"customId": "myCustomId1",
"name": "Item 1"
},
{
"id": 2,
"customId": "myCustomId2",
"name": "Item 2"
}
]
}
I think the best you can do here is using parameters handling for that request. For instance, you can do http://localhost:5000/items?myCustomId=1 in your case. This will probably list all the items with customId of 1 instead of just 1 item (since customId is not unique in this case).

Get value from exact JSONpath level

I want to extract values from an JSON document with using the path operators.
For example I get all the product IDs included in the file via $..product_id.
But for getting the "id" when I use $..id I get an output for each id element, no matter on which level of the JSON the variable is.
For example in my output I get an row for the id "12345678" as well as for "11223344" which should not be because it is a subset of the first ID.
{
"next_offset": 20,
"records": [
{
"id": "12345678",
"date": "2020-02-14",
"product_id": "asdf1234",
"product_name": "Product_test^_1",
"template_link": {
"name": "aassddff",
"id": "11223344",
"_acl": {
"fields": [],
"_hash": "345thvz356b56v456b"
}
},
....
}
]
}
How can I set the path operator to only access the "id" fields of one specific level?
For the JSON shown in your question, use $.records.*.id.

Validate JSON schema for expected values

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.

Access deeper elements of a JSON using postgresql 9.4

I want to be able to access deeper elements stored in a json in the field json, stored in a postgresql database. For example, I would like to be able to access the elements that traverse the path states->events->time from the json provided below. Here is the postgreSQL query I'm using:
SELECT
data#>> '{userId}' as user,
data#>> '{region}' as region,
data#>>'{priorTimeSpentInApp}' as priotTimeSpentInApp,
data#>>'{userAttributes, "Total Friends"}' as totalFriends
from game_json
WHERE game_name LIKE 'myNewGame'
LIMIT 1000
and here is an example record from the json field
{
"region": "oh",
"deviceModel": "inHouseDevice",
"states": [
{
"events": [
{
"time": 1430247045.176,
"name": "Session Start",
"value": 0,
"parameters": {
"Balance": "40"
},
"info": ""
},
{
"time": 1430247293.501,
"name": "Mission1",
"value": 1,
"parameters": {
"Result": "Win ",
"Replay": "no",
"Attempt Number": "1"
},
"info": ""
}
]
}
],
"priorTimeSpentInApp": 28989.41467999999,
"country": "CA",
"city": "vancouver",
"isDeveloper": true,
"time": 1430247044.414,
"duration": 411.53,
"timezone": "America/Cleveland",
"priorSessions": 47,
"experiments": [],
"systemVersion": "3.8.1",
"appVersion": "14312",
"userId": "ef617d7ad4c6982e2cb7f6902801eb8a",
"isSession": true,
"firstRun": 1429572011.15,
"priorEvents": 69,
"userAttributes": {
"Total Friends": "0",
"Device Type": "Tablet",
"Social Connection": "None",
"Item Slots Owned": "12",
"Total Levels Played": "0",
"Retention Cohort": "Day 0",
"Player Progression": "0",
"Characters Owned": "1"
},
"deviceId": "ef617d7ad4c6982e2cb7f6902801eb8a"
}
That SQL query works, except that it doesn't give me any return values for totalFriends (e.g. data#>>'{userAttributes, "Total Friends"}' as totalFriends). I assume that part of the problem is that events falls within a square bracket (I don't know what that indicates in the json format) as opposed to a curly brace, but I'm also unable to extract values from the userAttributes key.
I would appreciate it if anyone could help me.
I'm sorry if this question has been asked elsewhere. I'm so new to postgresql and even json that I'm having trouble coming up with the proper terminology to find the answers to this (and related) questions.
You should definitely familiarize yourself with the basics of json
and json functions and operators in Postgres.
In the second source pay attention to the operators -> and ->>.
General rule: use -> to get a json object, ->> to get a json value as text.
Using these operators you can rewrite your query in the way which returns correct value of 'Total Friends':
select
data->>'userId' as user,
data->>'region' as region,
data->>'priorTimeSpentInApp' as priotTimeSpentInApp,
data->'userAttributes'->>'Total Friends' as totalFriends
from game_json
where game_name like 'myNewGame';
Json objects in square brackets are elements of a json array.
Json arrays may have many elements.
The elements are accessed by an index.
Json arrays are indexed from 0 (the first element of an array has an index 0).
Example:
select
data->'states'->0->'events'->1->>'name'
from game_json
where game_name like 'myNewGame';
-- returns "Mission1"
select
data->'states'->0->'events'->1->>'name'
from game_json
where game_name like 'myNewGame';
This did help me