I have the following json output:
[
{
"serviceid": "service1",
"endpoints" : {
"endpoint1": [
"example1",
"example2",
"example3",
"example4"
],
"endpoint2": [
"example3",
"example4",
"example5",
"example6"
]
},
"version": "1.0"
},
{
"serviceid": "service2",
"endpoints" : {
"endpoint3": [
"example7",
"example8",
"example9",
"example10"
]
},
"version": "2.0"
}
]
So I need to remove endpoint1, endpoint2 and endpoint3 and preserve their children as array.
This is the desired output:
[
{
"serviceid": "service1",
"endpoints" : [
"example1",
"example2",
"example3",
"example4"
"example3",
"example4",
"example5",
"example6"
],
"version": "1.0"
},
"serviceid": "service2",
"endpoints" : [
"example7",
"example8",
"example9",
"example10"
],
"version": "2.0"
}
]
I don't have any idea to face it, please could you help me?
Thanks in advance.
Once you've fixed all the typos in your input (several commas and some curly braces were missing), this should work:
jq 'map(.endpoints |= [.[][]])'
Demo
Related
I am trying to create a .json file. The third element should be a list with lists as elements.
What am I doing wrong?
Bellow is the json file I created with R:
{
"list1": [
"element1"
],
"list2": [
"element2"
],
"List_with_lists_as_elements": [
"Child1":{
"Name": "Child1",
"Child1_Title": [
"Title1",
"Title2",
"Title3"]
,
"Child1_Subtitle": [
"Subtitle_1",
"Subtitle_2",
"Subtitle_3"
]
},
"Child2":{
"Name": "Child2",
"Child2_Title": [
"Title1",
"Title2",
"Title3"]
,
"Child2_Subtitle": [
"Subtitle2_1",
"Subtitle2_2",
"Subtitle2_3"
]
},
"Child3":{
"Name": "Child3",
"Child2_Title": [
"Title1",
"Title2",
"Title3"]
,
"Child2_Subtitle": [
"Subtitle3_1",
"Subtitle3_2",
"Subtitle3_3"
]
}
]
}
I then save this as example_json.json and upload using fromJSON(txt = 'example_json.json'), and I have a error message, probably because I dont know quite well create a .json file:
Error in parse_con(txt, bigint_as_char) :
parse error: after array element, I expect ',' or ']'
_as_elements": [ "Child1":{ "Name": "Child1",
(right here) ------^
How can I create a .json file that gives me a list with lists() ?
The issue is that you have keys in your array :
...
"List_with_lists_as_elements": [
"Child1":{
"Name": "Child1",
...
},
"Child2":{
"Name": "Child2",
...
},
"Child3":{
"Name": "Child3",
...
}
]
...
You have a Name field which contains the key values, so you can probably just remove the keys:
...
"List_with_lists_as_elements": [
{
"Name": "Child1",
...
},
{
"Name": "Child2",
...
},
{
"Name": "Child3",
...
}
]
...
I want to send slack notifications to a channel as soon as any log with loglevel ERROR appears in my index. I have configured watcher in the following way but it is giving me errors. The slack message must have the log message.
I am not able to configure this exactly.
{
"trigger": {
"schedule": {
"interval": "10s"
}
},
"input": {
"search": {
"request": {
"search_type": "query_then_fetch",
"indices": [
"index-log*",
"index-beat*"
],
"rest_total_hits_as_int": true,
"body": {
"query": {
"match": {
"loglevel": "ERROR"
},
"range": {
"#timestamp": {
"from": "{{ctx.trigger.scheduled_time}}||-5m",
"to": "{{ctx.trigger.triggered_time}}"
}
}
}
}
}
}
},
"condition": {
"compare": {
"ctx.payload.hits.total": {
"gt": 0
}
}
},
"actions": {
"send_trigger": {
"webhook": {
"scheme": "https",
"host": "hooks.slack.com",
"port": 443,
"method": "post",
"path": "/services/XXXX/XXXX/XXXX",
"params": {},
"headers": {
"Content-type": "application/json"
},
"body": """{ "text": "{{ctx.payload}}"}"""
}
}
}
}
below is the structure of my logs in kibana
{
"_index": "index-beat",
"_type": "_doc",
"_id": "P3Toa34B1LVeuWotaVOY",
"_version": 1,
"_score": 1,
"_source": {
"#timestamp": "2022-01-18T06:38:19.559Z",
"name": "communication",
"loglevel": "ERROR",
"log": {
"file": {
"path": "/home/ubuntu/abc/abc/logs/communication.log"
},
"offset": 0
},
"timestamp": "2022-01-18T06:38:15.384279",
"exception": {
"ex_type": "None",
"ex": "None",
"tb": ""
},
"message": "{'err': 'Test'}"
},
"fields": {
"exception.ex_type": [
"None"
],
"loglevel.keyword": [
"ERROR"
],
"name.keyword": [
"communication"
],
"log.offset": [
0
],
"message": [
"{'err': 'Test'}"
],
"exception.tb": [
""
],
"exception.ex": [
"None"
],
"#timestamp": [
"2022-01-18T06:38:19.559Z"
],
"exception.tb.keyword": [
""
],
"loglevel": [
"ERROR"
],
"log.file.path": [
"/home/ubuntu/abc/abc/logs/communication.log"
],
"message.keyword": [
"{'err': 'Test'}"
],
"name": [
"communication"
],
"exception.ex_type.keyword": [
"None"
],
"exception.ex.keyword": [
"None"
],
"log.file.path.keyword": [
"/home/ubuntu/abc/abc/logs/communication.log"
],
"timestamp": [
"2022-01-18T06:38:15.384Z"
]
}
}
Please help me out in this one.
I have the following json input from a internet service:
{
"sunarme": "foo",
"id": "foo-id",
"name": "Foo bar",
"profile": [
{
"id": "test1",
"products": [
"product1",
"product2"
],
"description": "test1 description"
},
{
"id": "test2",
"products": [
"product3",
"product4",
"product5"
],
"description": "test2 description"
},
{
"id": "test3",
"products": [
"product6",
"product7",
"product8"
],
"description": "test2 description"
}
]
}
So I need to transform profile key from array to json object. This is the desired output:
{
"sunarme": "foo",
"id": "foo-id",
"name": "Foo bar",
"profile": {
"test1": [
"product1",
"product2"
],
"test2": [
"product3",
"product4",
"product5"
],
"test3": [
"product6",
"product7",
"product8"
]
}
}
I don't have any idea how to do it in jq command, please, could you help me?
Thanks in advance.
Use with_entries which lets you convert the array into an object if you adjust the .keys accordingly.
jq '.profile |= with_entries(.key = .value.id | .value |= .products)'
Demo
Or use reduce to build the object by iterating through the array.
jq '.profile |= reduce .[] as $p ({}; .[$p.id] = $p.products)'
Demo
Or use map to convert each array item into an object, then merge them using add.
jq '.profile |= (map({(.id): .products}) | add)'
Demo
Output is:
{
"sunarme": "foo",
"id": "foo-id",
"name": "Foo bar",
"profile": {
"test1": [
"product1",
"product2"
],
"test2": [
"product3",
"product4",
"product5"
],
"test3": [
"product6",
"product7",
"product8"
]
}
}
I have this input json as example:
INPUT
{
"service1": {
"action1": [
"example1"
],
"action2": [
"example2",
"example3",
"example4"
]
},
"service2": {
"action3": [
"example5"
],
"action4": [
"example6",
"example7",
"example8"
]
}
}
I need to remove parents: action1, action2, action3, action4 and preserve their children as join, is there a way to do that in jq ?
DESIRED OUTPUT
{
"service1": [
"example1",
"example2",
"example3",
"example4"
],
"service2": [
"example5",
"example6",
"example7",
"example8"
]
}
jq '.[] |= add'
Will generate
{
"service1": [
"example1",
"example2",
"example3",
"example4"
],
"service2": [
"example5",
"example6",
"example7",
"example8"
]
}
As you can try in this only demo
For add, please take a look at jq's documentation.
I have to draw some lines by OpenLayers. The line features are coded as GeoJSON format. My code is ok for hard coded GeoJSON features. But, if I put this features in separate file and try to load it. It just does not work. I do not know what is the wrong with my loading external GeoJSON file. I have given both the code.
Code 1:
// This code is ok with hard coded GeoJSON features
map.addControl(new OpenLayers.Control.LayerSwitcher());
vectorLayer = new OpenLayers.Layer.Vector("Lines");
var myGeoJSON = { "type": "FeatureCollection",
"features":
[
{ "type": "Feature", "properties": { "LENGTH": 756.304000}, "geometry": { "type": "LineString", "coordinates": [ [ 18.105018, 59.231027 ], [ 18.104176, 59.230737 ], [ 18.103928, 59.230415 ], [ 18.103650, 59.230336 ], [ 18.103028, 59.230463 ], [ 18.102491, 59.230418 ], [ 18.101976, 59.230237 ], [ 18.100893, 59.230110 ], [ 18.100117, 59.230016 ], [ 18.097715, 59.230262 ], [ 18.096907, 59.230376 ], [ 18.096637, 59.230405 ], [ 18.096578, 59.230428 ], [ 18.096429, 59.230450 ], [ 18.096336, 59.230479 ], [ 18.096108, 59.230534 ], [ 18.095971, 59.230600 ], [ 18.095925, 59.230633 ], [ 18.095891, 59.230665 ], [ 18.094000, 59.231676 ], [ 18.093864, 59.231720 ] ] } }
,
{ "type": "Feature", "properties": { "LENGTH": 1462.390000}, "geometry": { "type": "LineString", "coordinates": [ [ 17.877073, 59.461653 ], [ 17.877116, 59.461598 ], [ 17.876936, 59.461507 ], [ 17.876936, 59.461323 ], [ 17.876773, 59.461098 ], [ 17.876430, 59.460885 ], [ 17.876413, 59.460553 ], [ 17.876576, 59.460280 ], [ 17.876575, 59.460078 ], [ 17.876762, 59.460060 ], [ 17.877371, 59.460042 ], [ 17.877808, 59.460046 ], [ 17.878641, 59.460046 ], [ 17.879010, 59.460078 ], [ 17.879337, 59.460044 ], [ 17.879526, 59.459878 ], [ 17.879749, 59.459563 ], [ 17.880058, 59.459538 ], [ 17.880435, 59.459503 ], [ 17.887550, 59.453608 ], [ 17.887696, 59.453430 ], [ 17.887971, 59.453150 ], [ 17.888221, 59.452843 ], [ 17.888246, 59.452721 ], [ 17.888435, 59.452609 ], [ 17.888470, 59.452568 ], [ 17.888517, 59.452410 ] ] } }
]
};
var geojson_format = new OpenLayers.Format.GeoJSON({
'internalProjection': map.baseLayer.projection,
'externalProjection': new OpenLayers.Projection("EPSG:4326")
});
map.addLayer(vectorLayer);
vectorLayer.addFeatures(geojson_format.read(myGeoJSON));
map.setCenter(
new OpenLayers.LonLat(18.068611, 59.329444).transform(
new OpenLayers.Projection("EPSG:4326"),
map.getProjectionObject()
), 10
);
Code 2: This code shows an error that it could not load features
//This code does not work because it can not load the external GeoJSON file
map.addControl(new OpenLayers.Control.LayerSwitcher());
vectorLayer = new OpenLayers.Layer.Vector("Lines");
var myGeoJSON = new OpenLayers.Layer.Vector("Lines", {
strategies: [new OpenLayers.Strategy.Fixed()],
protocol: new OpenLayers.Protocol.HTTP({
url: "ml/lines.json"
})
});
var geojson_format = new OpenLayers.Format.GeoJSON({
'internalProjection': map.baseLayer.projection,
'externalProjection': new OpenLayers.Projection("EPSG:4326")
});
map.addLayer(vectorLayer);
vectorLayer.addFeatures(geojson_format.read(myGeoJSON));
map.setCenter(
new OpenLayers.LonLat(18.068611, 59.329444).transform(
new OpenLayers.Projection("EPSG:4326"),
map.getProjectionObject()
), 10
);
Thanks in advance
geojson_layer = new OpenLayers.Layer.Vector("GeoJSON", {
strategies: [new OpenLayers.Strategy.Fixed()],
protocol: new OpenLayers.Protocol.HTTP({
url: "ml/lines.json",
format: new OpenLayers.Format.GeoJSON()
})
});
See my little example.