How to perform Jolt Spec operation in nested Json - json

I am trying to write a Jolt Spec on nested json of following input record. There is a challenge in this...objectname is not always have multiple object like below, in some case it contain single object, and we have to parse this too.
Input
{
"Status": "Green",
"objectname": {
"LED TV": {
"values": [
[
"one",
"two",
"three",
"four"
],
[
0,
0,
0,
"one"
],
[
0,
"one",
0,
"two"
]
],
"time": [
1663331241000,
1663330155000,
1663328545000
]
},
"LED Bulb": {
"values": [
[
"one",
"two",
"three",
"four"
],
[
0,
0,
0,
"one"
],
[
0,
"one",
0,
"two"
]
],
"time": [
1663331241000,
1663330155000,
1663328545000
]
},
"LED LAMP": {
"values": [
[
"one",
"two",
"three",
"four"
],
[
0,
0,
0,
"one"
],
[
0,
"one",
0,
"two"
]
],
"time": [
1663331541000,
1663330555000,
1663328545000
]
}
},
"Source": "LED EQUIPS"
}
Expected Output
[
[
"Status",
"Green",
"objectname",
"LED TV",
"values",
[
"one",
"two",
"three",
"four"
],
[
0,
0,
0,
"one"
],
[
0,
"one",
0,
"two"
]
],
[
"Status",
"Green",
"objectname",
"LED Bulb",
"values",
[
"one",
"two",
"three",
"four"
],
[
0,
0,
0,
"one"
],
[
0,
"one",
0,
"two"
]
],
[
"Status",
"Green",
"objectname",
"LED LAMP",
"values",
[
"one",
"two",
"three",
"four"
],
[
0,
0,
0,
"one"
],
[
0,
"one",
0,
"two"
]
],
"Source",
"LED EQUIPS"
]
PS : we have to parse "objectname" if they have one or multiple entries in it.

You can use $ wildcards to get keys of the attributes or objects for several layers, and use [# ] wildcards to generate respective arrays from the derived values from the nodes such as
[
{
"operation": "shift",
"spec": {
"o*": { // filter for "objectname"
"*": {
"v*": { // filter for "values"
"#Status": "[#3]", // fixed value "Status" in order to repeat within the objects
"#(3,Status)": "[#3]", // pick the value of the Status attribute after going three levels up the tree
"$2": "[#3]", // collect the key names for each object under related "objectname"
"$1": "[#3]",
"$": "[#3]",
"*": {
"#": "[#4]"
}
}
}
}
}
}
]

Related

Jsonpath: can not get the needed value using expressions

This is the example JSON
{
"customerId": 0,
"authorizedCustomerIds": [
0
],
"creativeVersionId": 0,
"description": "string",
"version": "string",
"duration": 0,
"frameRate": 0,
"previewImage": "string",
"previewVideo": "string",
"renderScript": "string",
"renderScriptType": "string",
"elements": [
{
"id": 0,
"type": "IMAGE",
"name": "string1",
"parentId": 0,
"presetId": 0,
"properties": [
{
"name": "string",
"value": {},
"type": "ALPHABETIC",
"producesVideo": true,
"renderScriptId": "string"
}
]
},
{
"id": 1,
"type": "TEXT",
"name": "string2",
"parentId": 0,
"presetId": 0,
"properties": [
{
"name": "string",
"value": {},
"type": "ALPHABETIC",
"producesVideo": false,
"renderScriptId": "string"
}
]
}
],
"globalElements": [
{
"id": 0,
"disabled": true
}
],
"shots": [
{
"name": "string",
"displayOrder": 0,
"startFrame": 0,
"duration": 0,
"thumbnailFrame": 0,
"elements": [
{
"id": 0,
"disabled": true
}
]
}
],
"sizes": [
{
"name": "string",
"displayOrder": 0,
"main": true,
"width": 0,
"height": 0,
"properties": [
{
"templateElementId": 0,
"propertyName": "string",
"linked": true,
"value": {}
}
]
}
]
}
I need to get the element name which type is TEXT and the properties "producesVideo" is false.
I tried this way but it not works
$..elements[?(#.type == 'TEXT' && #.properties[?(#.producesVideo == false)])].name
Then I need to extract the path with rest assured
.extract().path();
using find or findAll conditions
What JSONPath implementation are you using? For that document and JSONPath expression, some implementations, e.g. jayway Java and jsoncons C++, will return the value result
[
"string2"
]
or the path result
[
"$['elements'][1]['name']"
]
Other implementations, e.g. Goessner Javascript, can't digest a filter expression with a subfilter. Christoph Burgmer's JSONPath Comparison covers that case here.
using rest assured find and findAll expressions the jsonpath should be like this:
.extract().path("elements.findAll{it.type == 'TEXT' && it.properties.find{it.producesVideo == false}}.name");

read hyperopt parameters from json

I want to read hyperopt parameters from a JSON file.
My JSON file would be like:
[
{
"id": "121",
"model": [
{
"model_name": "power",
"estimator_type": [
{
"type": "Polynomial",
"degree": [2, 3, 4]
},
{
"type": "svm",
"C": [0, 1],
"kernel": [
{
"ktype": "linear"
},
{
"ktype": "RBF",
"width": [0, 1]
}
]
}
],
"cut_values": {
"qids": ["1234"]
}
},
{
"model_name": "speed",
"estimator_type": [
{
"type": "Polynomial",
"degree": ["quniform", 2, 3]
}
],
"cut_values": null
}
]
},
{
"id": "123",
"model": [
{
"model_name": "power",
"estimator_type": [
{
"type": "LinearRegression"
}
],
"cut_values": null
}
]
}
]
I have checked this post but with no success for more complex JSON like the one above.
I want to be able to create a space like 2.2 A Search Space Example: scikit-learn.

Extract from json with | jq by a given word

Can somebody help me to extract with | jq the following:
{
"status": "success",
"data": {
"resultType": "matrix",
"result": [
{
"metric": {
"pod": "dev-cds-5c97cf7f78-sw6b9"
},
"values": [
[
1588204800,
"0.3561394483796914"
],
[
1588215600,
"0.3607968456046861"
],
[
1588226400,
"0.3813882532417868"
],
[
1588237200,
"0.6264355815408573"
]
]
},
{
"metric": {
"pod": "uat-cds-66ccc9685-b5tvh"
},
"values": [
[
1588204800,
"0.9969746974696218"
],
[
1588215600,
"0.7400881057270005"
],
[
1588226400,
"1.2298959318837195"
],
[
1588237200,
"0.9482296838254507"
]
]
}
]
}
}
I need to obtain all-values individually by given word dev-cds and not all the name dev-cds-5c97cf7f78-sw6b9.
Result desired:
{
"metric": {
"pod": "dev-cds-5c97cf7f78-sw6b9"
},
"values": [
[
1588204800,
"0.3561394483796914"
],
[
1588215600,
"0.3607968456046861"
],
[
1588226400,
"0.3813882532417868"
],
[
1588237200,
"0.6264355815408573"
]
]
}
You should first iterate over the result array. Check if the pod inside, metric object has the value that contains "dev-cds".
.data.result[] | if .metric.pod | contains("dev-cds") then . else empty end
https://jqplay.org/s/54OH83qHKP

graphiql only allows query if at least one element of each sublist contains queried field

I am using graphiQL to debug my react project's graphql query.
This is my query:
{
testdataJson{
E{
D{
A
B
entries
}
}
}
}
on dataset testdata.json:
{
"E": [
{
"D": [
[
{
"A": "Y1",
"entries": [
"Z1"
]
},
{
"A": "Y2",
"entries": [
"Z2"
]
}
],
[
{
"A": "Y3",
"entries": [
"Z3"
]
},
{
"A": "Y4",
"entries": [
"Z4"
],
"B": true
}
],
[
{
"A": "Y5",
"entries": [
"Z5"
],
"B": true
}
]
]
}
]
}
which gives the error: Cannot query field \"B\" on type \"TestdataJsonED\".
If I change the snippet:
[
{
"A": "Y1",
"entries": [
"Z1"
]
},
{
"A": "Y2",
"entries": [
"Z2"
]
}
],
to
[
{
"A": "Y1",
"entries": [
"Z1"
]
},
{
"A": "Y2",
"entries": [
"Z2"
]
"B": true
}
],
then everything is fine.
This seems weird as I would expect to be able to have lists which don't necessarily contain at least one element with a "B" value. In fact, my JSON data (which I did not create) has this a lot. Alternatively, this is not the cause and I have done something silly!
Cheers,
Edit: related issue - https://github.com/gatsbyjs/gatsby/issues/2392

Use jq to find array element by containing string

I have an array "operations" from which I would like to return all the elements that contain a matching string like "w51". Until now, all the samples I have found dealt with key value pairs. I am using jq '.operations[]' < file to retrieve the elements.
{
"operations": [
[
"create",
"w51",
"rwt.widgets.Label",
{
"parent": "w41",
"style": [
"NONE"
],
"bounds": [
101,
0,
49,
42
],
"tabIndex": -1,
"customVariant": "variant_pufferLabelLogout"
}
],
[
"create",
"w39",
"rwt.widgets.Composite",
{
"parent": "w34",
"style": [
"NONE"
],
"children": [
"w52"
],
"bounds": [
0,
42,
762,
868
],
"tabIndex": -1,
"clientArea": [
0,
0,
762,
868
]
}
]
]
}
My expected output when searching for an array element that contains "w51" would be this:
[
"create",
"w51",
"rwt.widgets.Label",
{
"parent": "w41",
"style": [
"NONE"
],
"bounds": [
101,
0,
49,
42
],
"tabIndex": -1,
"customVariant": "variant_pufferLabelLogout"
}
]
If you use jq version 1.4 or later, the following should produce the desired output:
.operations[]
| select( index("w51") )
Alternatives
There are many alternatives, depending on which version of jq you have. If your jq has any/0, the following is an efficient option:
.operations[] | select( any(. == "w51" ) )