<div dir="ltr">i'm not sure that *format-flat-json* works with lists same as with nested dicts.<div>My example uses format-flat-json, but without an additional filter on pothon, I was unable to flatten it<br></div><div>There is original message<br><br><pre class="gmail-c-mrkdwn__pre" style="box-sizing:inherit;margin-top:4px;margin-bottom:4px;padding:8px;font-size:12px;line-height:1.50001;font-variant-ligatures:none;white-space:pre-wrap;word-break:normal;border-radius:4px;color:rgb(29,28,29);font-family:Monaco,Menlo,Consolas,"Courier New",monospace">{
"operation": "CLIENT_RESPONSE",
"identity": "<a href="http://ns-server.example.com">ns-server.example.com</a>",
"family": "INET",
"protocol": "TCP",
"query-ip": "192.168.xx.zz",
"query-port": "51000",
"response-ip": "192.168.yy.zz",
"response-port": "53",
"length": 691,
"rcode": "NOERROR",
"qname": "_<a href="http://dnsaddr.bootstrap.libp2p.io">dnsaddr.bootstrap.libp2p.io</a>",
"qtype": "TXT",
"latency": "0.000000",
"timestamp-rfc3339": "2022-06-06T08:47:58.797332215Z",
"answers": [{
"name": "_<a href="http://dnsaddr.bootstrap.libp2p.io">dnsaddr.bootstrap.libp2p.io</a>",
"rdatatype": "TXT",
"ttl": 600,
"rdata": "dnsaddr=/dnsaddr/<a href="http://ams-2.bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb">ams-2.bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb</a>"
}, {
"name": "_<a href="http://dnsaddr.bootstrap.libp2p.io">dnsaddr.bootstrap.libp2p.io</a>",
"rdatatype": "TXT",
"ttl": 600,
"rdata": "dnsaddr=/dnsaddr/<a href="http://sjc-1.bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN">sjc-1.bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN</a>"
}, {
"name": "_<a href="http://dnsaddr.bootstrap.libp2p.io">dnsaddr.bootstrap.libp2p.io</a>",
"rdatatype": "TXT",
"ttl": 600,
"rdata": "dnsaddr=/dnsaddr/<a href="http://ewr-1.bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa">ewr-1.bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa</a>"
}, {
"name": "_<a href="http://dnsaddr.bootstrap.libp2p.io">dnsaddr.bootstrap.libp2p.io</a>",
"rdatatype": "TXT",
"ttl": 600,
"rdata": "dnsaddr=/dnsaddr/<a href="http://ams-rust.bootstrap.libp2p.io/p2p/12D3KooWEZXjE41uU4EL2gpkAQeDXYok6wghN7wwNVPF5bwkaNfS">ams-rust.bootstrap.libp2p.io/p2p/12D3KooWEZXjE41uU4EL2gpkAQeDXYok6wghN7wwNVPF5bwkaNfS</a>"
}, {
"name": "_<a href="http://dnsaddr.bootstrap.libp2p.io">dnsaddr.bootstrap.libp2p.io</a>",
"rdatatype": "TXT",
"ttl": 600,
"rdata": "dnsaddr=/dnsaddr/<a href="http://sjc-2.bootstrap.libp2p.io/p2p/QmZa1sAxajnQjVM8WjWXoMbmPd7NsWhfKsPkErzpm9wGkp">sjc-2.bootstrap.libp2p.io/p2p/QmZa1sAxajnQjVM8WjWXoMbmPd7NsWhfKsPkErzpm9wGkp</a>"
}, {
"name": "_<a href="http://dnsaddr.bootstrap.libp2p.io">dnsaddr.bootstrap.libp2p.io</a>",
"rdatatype": "TXT",
"ttl": 600,
"rdata": "dnsaddr=/dnsaddr/<a href="http://nrt-1.bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt">nrt-1.bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt</a>"
}],
"country-isocode": "-"
}</pre>I would be very grateful to you if you have a solution using the built-in functions of syslog-ng.<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">пт, 10 июн. 2022 г. в 15:00, <<a href="mailto:syslog-ng-request@lists.balabit.hu">syslog-ng-request@lists.balabit.hu</a>>:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Send syslog-ng mailing list submissions to<br>
<a href="mailto:syslog-ng@lists.balabit.hu" target="_blank">syslog-ng@lists.balabit.hu</a><br>
<br>
To subscribe or unsubscribe via the World Wide Web, visit<br>
<a href="https://lists.balabit.hu/mailman/listinfo/syslog-ng" rel="noreferrer" target="_blank">https://lists.balabit.hu/mailman/listinfo/syslog-ng</a><br>
or, via email, send a message with subject or body 'help' to<br>
<a href="mailto:syslog-ng-request@lists.balabit.hu" target="_blank">syslog-ng-request@lists.balabit.hu</a><br>
<br>
You can reach the person managing the list at<br>
<a href="mailto:syslog-ng-owner@lists.balabit.hu" target="_blank">syslog-ng-owner@lists.balabit.hu</a><br>
<br>
When replying, please edit your Subject line so it is more specific<br>
than "Re: Contents of syslog-ng digest..."<br>
<br>
<br>
Today's Topics:<br>
<br>
1. need help with parser to make flat nested json list of<br>
dictionaries (????????? ???????????)<br>
2. Re: need help with parser to make flat nested json list of<br>
dictionaries (Peter Kokai (pkokai))<br>
<br>
<br>
----------------------------------------------------------------------<br>
<br>
Message: 1<br>
Date: Fri, 10 Jun 2022 11:02:55 +0300<br>
From: ????????? ??????????? <<a href="mailto:alexander.a.maslennikov@gmail.com" target="_blank">alexander.a.maslennikov@gmail.com</a>><br>
To: <a href="mailto:syslog-ng@lists.balabit.hu" target="_blank">syslog-ng@lists.balabit.hu</a><br>
Subject: [syslog-ng] need help with parser to make flat nested json<br>
list of dictionaries<br>
Message-ID:<br>
<CA+G0nAjp1b6_50LbCROVPje1_B4R_AzNYiZ-_dT0m=<a href="mailto:fXcqwmHA@mail.gmail.com" target="_blank">fXcqwmHA@mail.gmail.com</a>><br>
Content-Type: text/plain; charset="utf-8"<br>
<br>
hi all<br>
i have a json message that contains a nested json list of dicts<br>
<br>
{"a":1,"b":[{"c":1},{"c":2},{"c":3}]}<br>
<br>
i want to flat that message, so expected result looks like {<br>
"a": 1,<br>
"b_0_c": 1,<br>
"b_1_c": 2,<br>
"b_2_c": 3<br>
}<br>
<br>
My approach is a python implemented parser.<br>
Is it possible to achieve the same result using the built-in syslog-ng<br>
tools?<br>
My solution below<br>
<br>
@define kafka-implementation kafka-c<br>
<br>
python {<br>
<br>
import collections<br>
import json<br>
<br>
class FlattenedJson(object):<br>
<br>
def parse(self, log_message, flat_message=None):<br>
def flatten(d, parent_key='', sep='_'):<br>
items = []<br>
for k, v in d.items():<br>
new_key = parent_key + sep + k if parent_key else k<br>
if isinstance(v, collections.MutableMapping):<br>
items.extend(flatten(v, new_key, sep=sep).items())<br>
elif isinstance(v, list):<br>
for idx, value in enumerate(v):<br>
items.extend(flatten(value, new_key + sep +<br>
str(idx), sep).items())<br>
else:<br>
items.append((new_key, v))<br>
return dict(items)<br>
try:<br>
decoded_msg = json.loads(log_message['MESSAGE'].decode('utf-8'))<br>
flat_message = flatten(decoded_msg)<br>
final_message =<br>
str(json.dumps(flat_message)).encode(encoding='utf-8')<br>
log_message['MESSAGE'] = final_message<br>
except Exception as error:<br>
log_message['python_error'] = 'An exception occurred:<br>
{}'.format(error)<br>
return True<br>
};<br>
<br>
destination d_kafka_dnstap {<br>
kafka(<br>
topic("mytopic")<br>
bootstrap-servers("localhost:9092")<br>
message("$(format-flat-json --scope all-nv-pairs<br>
application_name=myapp @timestamp=${ISODATE} )")<br>
);<br>
};<br>
<br>
source s_net_dnstap { network( transport(udp) port(514) flags(no-parse) ); };<br>
<br>
parser p_dnstap { channel {<br>
parser { python(class("FlattenedJson")); };<br>
parser { json-parser(prefix("dnstap.")); };<br>
};<br>
};<br>
<br>
log { source(s_net_dnstap); parser(p_dnstap); destination(d_kafka_dnstap); };<br>
-------------- next part --------------<br>
An HTML attachment was scrubbed...<br>
URL: <<a href="http://lists.balabit.hu/pipermail/syslog-ng/attachments/20220610/0fb59c2a/attachment-0001.htm" rel="noreferrer" target="_blank">http://lists.balabit.hu/pipermail/syslog-ng/attachments/20220610/0fb59c2a/attachment-0001.htm</a>><br>
<br>
------------------------------<br>
<br>
Message: 2<br>
Date: Fri, 10 Jun 2022 08:09:24 +0000<br>
From: "Peter Kokai (pkokai)" <<a href="mailto:Peter.Kokai@oneidentity.com" target="_blank">Peter.Kokai@oneidentity.com</a>><br>
To: "<a href="mailto:syslog-ng@lists.balabit.hu" target="_blank">syslog-ng@lists.balabit.hu</a>" <<a href="mailto:syslog-ng@lists.balabit.hu" target="_blank">syslog-ng@lists.balabit.hu</a>><br>
Subject: Re: [syslog-ng] need help with parser to make flat nested<br>
json list of dictionaries<br>
Message-ID:<br>
<<a href="mailto:SA1PR19MB5641CF9A4E2AB1502C5AE348F8A69@SA1PR19MB5641.namprd19.prod.outlook.com" target="_blank">SA1PR19MB5641CF9A4E2AB1502C5AE348F8A69@SA1PR19MB5641.namprd19.prod.outlook.com</a>><br>
<br>
Content-Type: text/plain; charset="koi8-r"<br>
<br>
Hello,<br>
<br>
If the underlines are not a must in the key, yes you can use *format-flat-json* (it uses dot instead of underscore). It uses the same syntax as format-json.<br>
<br>
--<br>
Kokan<br>
<br>
________________________________________<br>
From: syslog-ng <<a href="mailto:syslog-ng-bounces@lists.balabit.hu" target="_blank">syslog-ng-bounces@lists.balabit.hu</a>> on behalf of ????????? ??????????? <<a href="mailto:alexander.a.maslennikov@gmail.com" target="_blank">alexander.a.maslennikov@gmail.com</a>><br>
Sent: 10 June 2022 10:02<br>
To: <a href="mailto:syslog-ng@lists.balabit.hu" target="_blank">syslog-ng@lists.balabit.hu</a><br>
Subject: [syslog-ng] need help with parser to make flat nested json list of dictionaries<br>
<br>
CAUTION: This email originated from outside of the organization. Do not follow guidance, click links, or open attachments unless you recognize the sender and know the content is safe.<br>
<br>
hi all<br>
i have a json message that contains a nested json list of dicts<br>
<br>
{"a":1,"b":[{"c":1},{"c":2},{"c":3}]}<br>
<br>
i want to flat that message, so expected result looks like {<br>
"a": 1,<br>
"b_0_c": 1,<br>
"b_1_c": 2,<br>
"b_2_c": 3<br>
}<br>
<br>
My approach is a python implemented parser.<br>
Is it possible to achieve the same result using the built-in syslog-ng tools?<br>
My solution below<br>
<br>
<br>
@define kafka-implementation kafka-c<br>
<br>
python {<br>
<br>
import collections<br>
import json<br>
<br>
class FlattenedJson(object):<br>
<br>
def parse(self, log_message, flat_message=None):<br>
def flatten(d, parent_key='', sep='_'):<br>
items = []<br>
for k, v in d.items():<br>
new_key = parent_key + sep + k if parent_key else k<br>
if isinstance(v, collections.MutableMapping):<br>
items.extend(flatten(v, new_key, sep=sep).items())<br>
elif isinstance(v, list):<br>
for idx, value in enumerate(v):<br>
items.extend(flatten(value, new_key + sep + str(idx), sep).items())<br>
else:<br>
items.append((new_key, v))<br>
return dict(items)<br>
try:<br>
decoded_msg = json.loads(log_message['MESSAGE'].decode('utf-8'))<br>
flat_message = flatten(decoded_msg)<br>
final_message = str(json.dumps(flat_message)).encode(encoding='utf-8')<br>
log_message['MESSAGE'] = final_message<br>
except Exception as error:<br>
log_message['python_error'] = 'An exception occurred: {}'.format(error)<br>
return True<br>
};<br>
<br>
destination d_kafka_dnstap {<br>
kafka(<br>
topic("mytopic")<br>
bootstrap-servers("localhost:9092")<br>
message("$(format-flat-json --scope all-nv-pairs application_name=myapp @timestamp=${ISODATE} )")<br>
);<br>
};<br>
<br>
source s_net_dnstap { network( transport(udp) port(514) flags(no-parse) ); };<br>
<br>
parser p_dnstap { channel {<br>
parser { python(class("FlattenedJson")); };<br>
parser { json-parser(prefix("dnstap.")); };<br>
};<br>
};<br>
<br>
log { source(s_net_dnstap); parser(p_dnstap); destination(d_kafka_dnstap); };<br>
<br>
<br>
------------------------------<br>
<br>
Subject: Digest Footer<br>
<br>
_______________________________________________<br>
syslog-ng maillist - <a href="mailto:syslog-ng@lists.balabit.hu" target="_blank">syslog-ng@lists.balabit.hu</a><br>
<a href="https://lists.balabit.hu/mailman/listinfo/syslog-ng" rel="noreferrer" target="_blank">https://lists.balabit.hu/mailman/listinfo/syslog-ng</a><br>
<br>
<br>
------------------------------<br>
<br>
End of syslog-ng Digest, Vol 206, Issue 2<br>
*****************************************<br>
</blockquote></div>