{"id":619,"date":"2017-11-12T18:05:09","date_gmt":"2017-11-12T18:05:09","guid":{"rendered":"https:\/\/projects.lsv.ens-paris-saclay.fr\/orchidsdoc\/?page_id=619"},"modified":"2017-12-08T10:51:05","modified_gmt":"2017-12-08T10:51:05","slug":"the-json-module","status":"publish","type":"page","link":"https:\/\/projects.lsv.ens-paris-saclay.fr\/orchidsdoc\/?page_id=619","title":{"rendered":"The json module"},"content":{"rendered":"<p>The <code>json<\/code> module is a <a title=\"Dissection modules\" href=\"https:\/\/projects.lsv.ens-paris-saclay.fr\/orchidsdoc\/?page_id=131\">dissection module<\/a>: its purpose is to take an Orchids event, parse its last field (which should be a text string, of type <code>str<\/code>), which should be in <a href=\"https:\/\/fr.wikipedia.org\/wiki\/JavaScript_Object_Notation\">JSON<\/a> format, and return a refined Orchids events, with additional fields.<\/p>\n<p>JSON means JavaScript Object Notation, and has become a popular alternative to XML for describing objects, possibly nested.\u00a0 The Linux <code>journalctl<\/code> utility, for example, has a native JSON export format.<\/p>\n<p>The <code>json<\/code> module is still in a preliminary version. Its intended use is to parse data embedded in, say, the <code>.syslog.msg<\/code>\u00a0field of an event already dissected once by the <a href=\"https:\/\/projects.lsv.ens-paris-saclay.fr\/orchidsdoc\/?page_id=157\"><code>syslog<\/code><\/a> module.\u00a0\u00a0 For example, if you know that messages reported by <a href=\"https:\/\/projects.lsv.ens-paris-saclay.fr\/orchidsdoc\/?page_id=157\"><code>syslog<\/code><\/a> is in JSON format when the <code>.syslog.prog<\/code> field equals <code>js_reporter<\/code> (that name is merely for the sake of the example), then you would write something like the following in <a href=\"https:\/\/projects.lsv.ens-paris-saclay.fr\/orchidsdoc\/?page_id=145\"><code>$OCONF\/orchids-inputs.conf<\/code><\/a>, assuming <a href=\"https:\/\/projects.lsv.ens-paris-saclay.fr\/orchidsdoc\/?page_id=157\"><code>syslog<\/code><\/a> data comes from the <a href=\"https:\/\/projects.lsv.ens-paris-saclay.fr\/orchidsdoc\/?page_id=87\"><code>textfile<\/code><\/a> module from file <code>blah.log<\/code>:<\/p>\n<pre>INPUT\t\ttextfile\t\"blah.log\"\r\nDISSECT syslog\ttextfile\t\"blah.log\"\r\nDISSECT json    syslog\t\tjs_reporter\r\n<\/pre>\n<h3>Configuration options<\/h3>\n<p>None (yet).<\/p>\n<h3>Fields<\/h3>\n<p>The fields provided by the <code>json<\/code> module are of a somewhat peculiar nature.\u00a0 Most other modules document a fixed set of fields.\u00a0 The <code>json<\/code> module has fields that may vary dynamically as events are obtained.\u00a0 To accomodate for this dynamicity, all those run-time fields are collected in one single static field, <code>.json.fields<\/code>, which is an <a href=\"https:\/\/projects.lsv.ens-paris-saclay.fr\/orchidsdoc\/?p=578\">array<\/a> mapping the names of the dynamic fields to their values.\u00a0 This will be clearer with examples, to be given below.\u00a0 We shall also explain the role of the <code>.json.remainder<\/code> field below.<\/p>\n<table style=\"border: solid 1px black;\">\n<tbody>\n<tr style=\"background-color: lightsteelblue;\">\n<th>Field<\/th>\n<th><a title=\"Types\" href=\"https:\/\/projects.lsv.ens-paris-saclay.fr\/orchidsdoc\/?page_id=98\">Type<\/a><\/th>\n<th><a href=\"https:\/\/projects.lsv.ens-paris-saclay.fr\/orchidsdoc\/?page_id=719\">Mono<\/a>?<\/th>\n<th>Description<\/th>\n<\/tr>\n<\/tbody>\n<tbody>\n<tr style=\"background-color: lightgrey;\">\n<td><code>.json.remainder<\/code><\/td>\n<td><code>str<\/code><\/td>\n<td><\/td>\n<td>rest of message<\/td>\n<\/tr>\n<tr style=\"background-color: white;\">\n<td><code>.json.fields<\/code><\/td>\n<td><code>[str<\/code><\/td>\n<td><\/td>\n<td>array of dynamic fields<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>(The type <code>[str<\/code> means array of strings. An array can be indexed by any basic type. Here the indices will be strings as well.)<\/p>\n<p>We start with a simple example.\u00a0 Imagine the following text, in JSON format:<\/p>\n<pre>{\"syslog_time\":\"2017-05-03T20:41:14.342405+02:00\",\"syslog_host\":\"darkstar\",\"process_id\":\"237\",\"path\":\"\/tmp\/exploit\"}<\/pre>\n<p>The <code>json<\/code> module will dissect that by letting <code>.json.fields<\/code> be the array that maps:<\/p>\n<ul>\n<li><code>\"syslog_time\"<\/code> to the value <code>\"2017-05-03T20:41:14.342405+02:00\"<\/code>;<br \/>\nnote that the latter is not a <code>ctime<\/code> value: to obtain one, use the <code>ctime_from_str<\/code> <a href=\"https:\/\/projects.lsv.ens-paris-saclay.fr\/orchidsdoc\/?page_id=246\">primitive<\/a>, for example;<\/li>\n<li><code>\"syslog_host\"<\/code> to the value <code>\"darkstar\"<\/code>;<\/li>\n<li><code>\"process_id\"<\/code> to the value <code>\"237\"<\/code>;<br \/>\nnote that the latter is not an <code>uint<\/code> value: to obtain one, use the <code>uint_from_str<\/code> <a href=\"https:\/\/projects.lsv.ens-paris-saclay.fr\/orchidsdoc\/?page_id=246\">primitive<\/a>, for example;<\/li>\n<li><code>\"path\"<\/code> to the value <code>\"\/tmp\/exploit\"<\/code>.<\/li>\n<\/ul>\n<p>The JSON format also allows one to nest objects, as in the <code>\"attributes\"<\/code> subobject below:<\/p>\n<pre>{\"syslog_time\":\"2017-05-03T20:41:14.342405+02:00\",\"syslog_host\":\"darkstar\",\"process_id\":\"237\",\"path\":\"\/tmp\/exploit\",\"attributes\":{\"length\":\"2\",\"referer\":\"apache\",\"object.class\":\"None\"}}<\/pre>\n<p>in which case the <code>.json.fields<\/code> array will contain the following additional fields:<\/p>\n<ul>\n<li><code>\"attributes.length\"<\/code>, mapped to the value <code>\"2\"<\/code>;<\/li>\n<li><code>\"attributes.referer\"<\/code>, mapped to the value <code>\"apache\"<\/code>;<\/li>\n<li><code>\"attributes.object.class\"<\/code>, mapped to the value <code>\"None\"<\/code>.<\/li>\n<\/ul>\n<p>Still more complicated, a JSON object may contain lists of sub-objects enclosed in between square brackets (&#8216;[&#8216;&#8230;&#8217;]&#8217;), as in the value of the <code>\"menu\"<\/code> subobject below.<\/p>\n<pre>{\"syslog_time\":\"2017-05-03T20:41:14.342405+02:00\",\"syslog_host\":\"darkstar\",\"process_id\":\"237\",\"path\":\"\/tmp\/exploit\",\"attributes\":{\"length\":\"2\",\"referer\":\"apache\",\"object.class\":\"None\"},\"menu\":[{\"value\":\"New\",\"onclick\":\"createNewDoc()\"},{\"value\":\"Open\",\"onclick\":\"OpenDoc()\"},{\"value\":\"Close\",\"onclick\":\"CloseDoc()\"}]}<\/pre>\n<p>in which case the <code>.json.fields<\/code> array will contain the following additional fields:<\/p>\n<ul>\n<li><code>\"menu(0).value\"<\/code>, mapped to\u00a0 <code>\"Open\"<\/code>;<\/li>\n<li><code>\"menu(0).onclick\"<\/code>, mapped to <code>\"createNewDoc()\"<\/code>;<\/li>\n<li><code>\"menu(1).value\"<\/code>, mapped to <code>\"Open\"<\/code>;<\/li>\n<li><code>\"menu(1).onclick\"<\/code>, mapped to <code>\"OpenDoc()\"<\/code>;<\/li>\n<li><code>\"menu(2).value\"<\/code>, mapped to <code>\"Close\"<\/code>;<\/li>\n<li><code>\"menu(2).onclick\"<\/code>, mapped to <code>\"CloseDoc()\"<\/code>.<\/li>\n<\/ul>\n<p>Note how list of objects are handled as though we had implicit subobjects, numbered 0, 1, 2, etc. \u00a0The syntax of fields is meant to be compatible with the accessors of the <a href=\"https:\/\/projects.lsv.ens-paris-saclay.fr\/orchidsdoc\/?page_id=345\"><code>prelude<\/code><\/a> module\u2014just so that you are not forced to get used to a new syntax when you switch modules.<\/p>\n<p>Finally, it may be that the JSON message ends in some junk, as exemplified at the end of the following.<\/p>\n<pre>{\"syslog_time\":\"2017-05-03T20:41:14.342405+02:00\",\"syslog_host\":\"darkstar\",\"process_id\":\"237\",\"path\":\"\/tmp\/exploit\",\"attributes\":{\"length\":\"2\",\"referer\":\"apache\",\"object.class\":\"None\"},\"menu\":[{\"value\":\"New\",\"onclick\":\"createNewDoc()\"},{\"value\":\"Open\",\"onclick\":\"OpenDoc()\"},{\"value\":\"Close\",\"onclick\":\"CloseDoc()\"}]}and then some junk<\/pre>\n<p>In that case, the <code>.json.remainder<\/code> fields will contain <code>\"and then some junk\"<\/code>.<\/p>\n<p>In all previous examples, <code>.json.remainder<\/code> simply contained the empty string <code>\"\"<\/code>.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The json module is a dissection module: its purpose is to take an Orchids event, parse its last field (which should be a text string, of type str), which should be in JSON format, and return a refined Orchids events, with additional fields. JSON means JavaScript Object Notation, and has become a popular alternative to &hellip; <a href=\"https:\/\/projects.lsv.ens-paris-saclay.fr\/orchidsdoc\/?page_id=619\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">The json module<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-619","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/projects.lsv.ens-paris-saclay.fr\/orchidsdoc\/index.php?rest_route=\/wp\/v2\/pages\/619","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/projects.lsv.ens-paris-saclay.fr\/orchidsdoc\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/projects.lsv.ens-paris-saclay.fr\/orchidsdoc\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/projects.lsv.ens-paris-saclay.fr\/orchidsdoc\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/projects.lsv.ens-paris-saclay.fr\/orchidsdoc\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=619"}],"version-history":[{"count":10,"href":"https:\/\/projects.lsv.ens-paris-saclay.fr\/orchidsdoc\/index.php?rest_route=\/wp\/v2\/pages\/619\/revisions"}],"predecessor-version":[{"id":728,"href":"https:\/\/projects.lsv.ens-paris-saclay.fr\/orchidsdoc\/index.php?rest_route=\/wp\/v2\/pages\/619\/revisions\/728"}],"wp:attachment":[{"href":"https:\/\/projects.lsv.ens-paris-saclay.fr\/orchidsdoc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}