I have the following input json, which has an array of json:
{
"ArrayList": [
{
"a": "value1",
"b": "value2",
"c": "value3"
},
{
"a": "value4",
"b": "value5",
"c": "value6"
},
{
"a": "value7",
"b": "value8",
"c": "value9"
}
]
}
And the desired output is :
{
"ArrayList": [
{
"a": "value1",
"b": "value2"
},
{
"a": "value4",
"b": "value5"
},
{
"a": "value7",
"b": "value8"
}
]
}
What will be the Jolt transform spec expression for this?
Seems you want to remove the attribute c of objects of the ArrayList array, then a remove transformation might be used such as
[
{
"operation": "remove",
"spec": {
"ArrayList": {
"*": {
"c": ""
}
}
}
}
]
Related
Still new to ES so apologies if this is obvious. Let's say I have a document with the structure like this
{..., 'objectArray': [{'a': 3, 'b': 2}, {'a': 0, 'b': 4}]}
in which my objectArray property is an array of objects. How would I query for documents in this index that have an object within objectArray with a = 3 and b = 4? So the above document would not be in the result set but
{..., 'objectArray': [{'a': 3, 'b': 2}, {'a': 3, 'b': 4}]}
would be
If you could show an example in NEST or just illustrate the type of query so I could read about it, that would be awesome, thanks so much
Assuming that we are talking about a property of type nested, we use nested query.
Mapping
PUT idx_nested
{
"mappings": {
"properties": {
"objectArray":{
"type": "nested"
}
}
}
}
Documents
POST idx_nested/_doc
{
"objectArray": [
{
"a": 3,
"b": 2
},
{
"a": 0,
"b": 4
}
]
}
POST idx_nested/_doc
{
"objectArray": [
{
"a": 3,
"b": 2
},
{
"a": 3,
"b": 4
}
]
}
Query
GET idx_nested/_search
{
"query": {
"nested": {
"path": "objectArray",
"query": {
"bool": {
"must": [
{
"term": {
"objectArray.a": {
"value": 3
}
}
},
{
"term": {
"objectArray.b": {
"value": 4
}
}
}
]
}
}
}
}
}
Response:
"hits": [
{
"_index": "idx_nested",
"_id": "4j5VxoQBiZR2Tvxo_zXz",
"_score": 2,
"_source": {
"objectArray": [
{
"a": 3,
"b": 2
},
{
"a": 3,
"b": 4
}
]
}
}
]
could anyone help to convert below mention input json to desired output json using jolt ?
Here in treefield list i want to fetch "paramid" and "paramvalue" to first level for only matched "paramid", rest of the list items should be intact in that treefield list.
e.g i want to take paramid "k1" with its value to first level as mentioned in the output.
Input
{
"A": "value1",
"B": "value2",
"C": {
"D": "x1",
"E": {
"treefield": [
{
"paramid": "k1",
"paramvalue": {
"string": "value1"
}
},
{
"paramid": "k2",
"paramvalue": {
"string": "value2"
}
},
{
"paramid": "k3",
"paramvalue": {
"string": "value3"
}
}
]
},
"F": {
"a": "x1",
"x": {
"y": 1
}
},
"H": "x4"
}
}
]```
**Output**
```[
{
"A": "value1",
"B": "value2",
"C": {
"D": "x1",
"E": {
"treefield": [
{
"paramid": "k1",
"paramvalue": {
"string": "value1"
}
},
{
"paramid": "k3",
"paramvalue": {
"string": "value3"
}
}
]
},
"F": {
"a": "x1",
"x": {
"y": 1
}
},
"H": "x4"
},
"k2": "value2"
}
]```
If I understand what you're trying to do (pull out key/value pairs and put them at the top level), this spec should do it:
[
{
"operation": "shift",
"spec": {
"C": {
"E": {
"treefield": {
"*": {
"paramvalue": {
"string": "#(2,paramid)"
}
}
}
},
"*": "&"
},
"*": "&"
}
}
]
That works on individual JSON objects. In your sample input you don't have a leading array bracket but you do have a trailing one, so if you expect multiple objects in an array and want them output in an array, this spec should work:
[
{
"operation": "shift",
"spec": {
"*": {
"C": {
"E": {
"treefield": {
"*": {
"paramvalue": {
"string": "[&6].#(2,paramid)"
}
}
}
},
"*": "[&2].&"
},
"*": "[&1].&"
}
}
}
]
I am trying to give as input a JSON that contains a fixed variable (in my example group) that has as value an imbrication of objects and my goal is to transform it to sub objects.
My Input :
[
{
"key": "name",
"value": "marc",
"group": "office.people"
}
]
My Spec :
[
{
"operation": "shift",
"spec": {
"*": {
"value": "#(1,key).#(1,group)"
}
}
}
]
Expected:
{
"name": {
"office": {
"people": "marc"
}
}
}
Actual:
{
"name" : {
"office.people" : "marc"
}
}
You could split the group first:
[
{
"operation": "modify-overwrite-beta",
"spec": {
"*": {
"group": "=split('\\.',#(1,group))"
}
}
},
{
"operation": "shift",
"spec": {
"*": {
"value": "#(1,key).#(1,group[0]).#(1,group[1])"
}
}
}
]
I have an Array of Jsons and I want to transform the keys of individual Jsons while still maintaining the Array.
[
{
"a": "1",
"b": "2"
},
{
"a": "one",
"b": "two"
}
]
Desired output:
[
{
"my_a": "1",
"my_b": "2"
},
{
"my_a": "one",
"my_b": "two"
}
]
JOLT Spec:
[
{
"operation": "shift",
"spec": {
"*": {
"a": "my_a",
"b": "my_b"
}
}
}
]
However, I see this:
{
"my_a" : [ "1", "one" ],
"my_b" : [ "2", "two" ]
}
I see that the transformation is applied but the output is not what I expect.
Anyone who has faced similar problems?
You need to include array index [&1] while changing the name
Try with below Jolt Spec:
[
{
"operation": "shift",
"spec": {
"*": {
"a": "[&1].my_a",
"b": "[&1].my_b"
}
}
}
]
Validating the Spec:
I am trying to perform conditional mapping of values from the following JSON.
My input,
{
"rating": [
{
"id": 1,
"locations": [
{
"num": 1
},
{
"num": 2
}
]
}
]
}
Expected output:
{
"rating": [
{
"id": 1,
"locations": [
{
"num": 1
}
],
"new_locations": [
{
"num": 2
}
]
}
]
}
My spec,
[
{
"operation": "shift",
"spec": {
"rating": {
"*": {
"locations": {
"*": {
"num": "#(3,id)"
}
}
}
}
}
}
]
If the num value matches with id,then it should stay in location array else should be moved to new_locations.
Can anyone please suggest me help.Thanks.
There isn't a way to do that kind of conditional matching logic with the "out of the box" Jolt transforms.