<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Parsing fields from json logs in Getting Data In</title>
    <link>https://community.splunk.com/t5/Getting-Data-In/Parsing-fields-from-json-logs/m-p/267993#M51422</link>
    <description>&lt;P&gt;Just tried that.&lt;BR /&gt;
I seem to get the following error:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Error in 'rex' command: Encountered the following error while compiling the regex '.*action\":\"(?&amp;lt;action&amp;gt;[^\]+)': Regex: missing terminating ] for character class 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Will post some samples of action field. (Need to to remove identifying data first) them first.&lt;/P&gt;</description>
    <pubDate>Fri, 21 Oct 2016 06:01:15 GMT</pubDate>
    <dc:creator>splunk_svc</dc:creator>
    <dc:date>2016-10-21T06:01:15Z</dc:date>
    <item>
      <title>Parsing fields from json logs</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Parsing-fields-from-json-logs/m-p/267991#M51420</link>
      <description>&lt;P&gt;Hi Splunkers.&lt;BR /&gt;
I'm attempting to search based on fields in a JSON log file&lt;BR /&gt;
For example I am trying to search based on the "action" field from the following (sample) JSON event:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;{"message":"{\"action\":\"USER_PROFILEACTION\"}","requestfrom":"source","responsestatus":"403","username":"user@name.com","station":"/level1/profile","resource":"/level1/profile","responsetime":275,"starttime":1476061950172,"finishtime":1476061950447} 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I've attempted to use spath and also a rex pipe but have had no luck.&lt;BR /&gt;
(i.e. here : &lt;A href="https://answers.splunk.com/answers/418995/how-to-extract-fields-from-json-which-is-stored-)in.html"&gt;https://answers.splunk.com/answers/418995/how-to-extract-fields-from-json-which-is-stored-)in.html&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;In this example it contains "USER_PROFILEACTION".&lt;BR /&gt;
Also note that the string in the action field also contains a trailing backslash at the end of the string.&lt;BR /&gt;
Preferably I'd like to strip this in the process.&lt;/P&gt;

&lt;P&gt;Any attempts I've made end up converting the field that the raw JSON log is stored in into a multivalue field, with a a second copy of the JSON log.&lt;/P&gt;

&lt;P&gt;Thanks in advance.&lt;/P&gt;</description>
      <pubDate>Fri, 21 Oct 2016 03:42:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Parsing-fields-from-json-logs/m-p/267991#M51420</guid>
      <dc:creator>splunk_svc</dc:creator>
      <dc:date>2016-10-21T03:42:42Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing fields from json logs</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Parsing-fields-from-json-logs/m-p/267992#M51421</link>
      <description>&lt;P&gt;Try this if there is only one \ in the string and that's at the end of action string. If there are other slashes then please paste some samples of action field so more apt regex can be built.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;yourBaseSearch
| rex field=_raw ".*action\\\":\\\"(?&amp;lt;action&amp;gt;[^\\]+)"
|complete your search
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG&gt;UPDATED SEARCH&lt;/STRONG&gt;&lt;BR /&gt;
If you want to extract all the key value pairs from the search and not just the action field then try this below search:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;yourBaseSearch
| rex max_match=0 field=_raw "\"(?&amp;lt;key&amp;gt;[^\"]+)\":(\"|\"{\\\")*(?&amp;lt;value&amp;gt;[^,]+)"
 | table key, value
 |  eval z=mvzip(key, value, "~") 
 | mvexpand z
 | table z
 |rex field=z "(?&amp;lt;key&amp;gt;[^~]+)~(?&amp;lt;value&amp;gt;.*)"
 | rex mode=sed field=value "s/{//g
 s/}//g
 s/\\\//g"
 |table key value 
 | rex max_match=0 field=value "\"(?&amp;lt;key&amp;gt;[^\"]+)\":\"(?&amp;lt;value&amp;gt;[^,]+)" 
 | table key, value
 |  eval z=mvzip(key, value, "~") 
 | mvexpand z
 | table z
 | rex field=z "(?&amp;lt;key&amp;gt;[^~]+)~(?&amp;lt;value&amp;gt;.*)"
 | table key, value
 | rex mode=sed field=value "s/\"//g"
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 21 Oct 2016 05:02:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Parsing-fields-from-json-logs/m-p/267992#M51421</guid>
      <dc:creator>gokadroid</dc:creator>
      <dc:date>2016-10-21T05:02:23Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing fields from json logs</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Parsing-fields-from-json-logs/m-p/267993#M51422</link>
      <description>&lt;P&gt;Just tried that.&lt;BR /&gt;
I seem to get the following error:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Error in 'rex' command: Encountered the following error while compiling the regex '.*action\":\"(?&amp;lt;action&amp;gt;[^\]+)': Regex: missing terminating ] for character class 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Will post some samples of action field. (Need to to remove identifying data first) them first.&lt;/P&gt;</description>
      <pubDate>Fri, 21 Oct 2016 06:01:15 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Parsing-fields-from-json-logs/m-p/267993#M51422</guid>
      <dc:creator>splunk_svc</dc:creator>
      <dc:date>2016-10-21T06:01:15Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing fields from json logs</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Parsing-fields-from-json-logs/m-p/267994#M51423</link>
      <description>&lt;P&gt;Issue is you didn't copy the box properly as there is one \ missing inside the &lt;CODE&gt;[^\]&lt;/CODE&gt; box.&lt;/P&gt;

&lt;P&gt;put two slashes in the box &lt;CODE&gt;[^\\]+)&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;The data if is exactly like you have put in question should work with the above regex, unless there are no slashes in your json data. However try this below query that should extract all the fields for you. Updating this in answer above as well:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;yourBaseSearch    
| rex max_match=0 field=_raw "\"(?&amp;lt;key&amp;gt;[^\"]+)\":(\"|\"{\\\")*(?&amp;lt;value&amp;gt;[^,]+)"
    | table key, value
    |  eval z=mvzip(key, value, "~") 
    | mvexpand z
    | table z
    |rex field=z "(?&amp;lt;key&amp;gt;[^~]+)~(?&amp;lt;value&amp;gt;.*)"
    | rex mode=sed field=value "s/{//g
    s/}//g
    s/\\\//g"
    |table key value 
    | rex max_match=0 field=value "\"(?&amp;lt;key&amp;gt;[^\"]+)\":\"(?&amp;lt;value&amp;gt;[^,]+)" 
    | table key, value
    |  eval z=mvzip(key, value, "~") 
    | mvexpand z
    | table z
    | rex field=z "(?&amp;lt;key&amp;gt;[^~]+)~(?&amp;lt;value&amp;gt;.*)"
    | table key, value
    | rex mode=sed field=value "s/\"//g"
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 21 Oct 2016 06:17:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Parsing-fields-from-json-logs/m-p/267994#M51423</guid>
      <dc:creator>gokadroid</dc:creator>
      <dc:date>2016-10-21T06:17:28Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing fields from json logs</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Parsing-fields-from-json-logs/m-p/267995#M51424</link>
      <description>&lt;P&gt;Back slash is confusing. &lt;BR /&gt;
Splunk is supposed to detect &lt;STRONG&gt;json&lt;/STRONG&gt; format. &lt;/P&gt;

&lt;P&gt;So, in your case, message field should be populated as follows;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;message = {"action":"USER_PROFILEACTION"}
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG&gt;Note: backslash in _raw exists while json field extraction removes it as it is escaping double-quote(").&lt;/STRONG&gt;&lt;/P&gt;

&lt;P&gt;In that case, the following rex should populate &lt;STRONG&gt;action=USER_PROFILEACTION&lt;/STRONG&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| rex field=message ":\"(?&amp;lt;action&amp;gt;.*)\""
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Ideally, your message value makes use of  json format like this&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;"message": {"action": "USER_PROFILEACTION" },
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;then, Splunk automatically extracts &lt;STRONG&gt;message.action=USER_PROFILEACTION&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Oct 2016 21:16:16 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Parsing-fields-from-json-logs/m-p/267995#M51424</guid>
      <dc:creator>Masa</dc:creator>
      <dc:date>2016-10-26T21:16:16Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing fields from json logs</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Parsing-fields-from-json-logs/m-p/267996#M51425</link>
      <description>&lt;P&gt;Thanks gokadroid.&lt;BR /&gt;
I'll put this down to a copy and paste snafu.&lt;BR /&gt;
The forum is removing the second backslash and needs an additional one used to escape the first one.&lt;/P&gt;

&lt;P&gt;Will have a bit of a play with your revised search string.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Oct 2016 03:09:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Parsing-fields-from-json-logs/m-p/267996#M51425</guid>
      <dc:creator>splunk_svc</dc:creator>
      <dc:date>2016-10-27T03:09:42Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing fields from json logs</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Parsing-fields-from-json-logs/m-p/267997#M51426</link>
      <description>&lt;P&gt;Unfortunately Masa, that is the way the data is being presented.&lt;BR /&gt;
We don't have the opportunity to modify the source to exclude the extra characters.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Oct 2016 03:10:47 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Parsing-fields-from-json-logs/m-p/267997#M51426</guid>
      <dc:creator>splunk_svc</dc:creator>
      <dc:date>2016-10-27T03:10:47Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing fields from json logs</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Parsing-fields-from-json-logs/m-p/267998#M51427</link>
      <description>&lt;P&gt;For what it's worth, I had success using XMLKV on one of my JSON feeds. &lt;/P&gt;

&lt;P&gt;&lt;A href="https://docs.splunk.com/Documentation/Splunk/6.5.0/SearchReference/Xmlkv"&gt;https://docs.splunk.com/Documentation/Splunk/6.5.0/SearchReference/Xmlkv&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Oct 2016 03:36:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Parsing-fields-from-json-logs/m-p/267998#M51427</guid>
      <dc:creator>JDukeSplunk</dc:creator>
      <dc:date>2016-10-27T03:36:34Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing fields from json logs</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Parsing-fields-from-json-logs/m-p/267999#M51428</link>
      <description>&lt;P&gt;Have been comparing results of original and full search that  gokadroid supplied.&lt;/P&gt;

&lt;P&gt;Still get the following error when using the smaller of the two searches.  i.e. to get just the message field.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Error in 'rex' command: Encountered the following error while compiling the regex '.*action\":\"(?&amp;lt;action&amp;gt;[^\]+)': Regex: missing terminating ] for character class.
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The actual query itself  &lt;EM&gt;does&lt;/EM&gt; contain [^\] in it.&lt;BR /&gt;
It's only the error message that Splunk returns that has the single backslash in it.&lt;BR /&gt;
i.e. "[^\]"&lt;/P&gt;</description>
      <pubDate>Thu, 27 Oct 2016 04:10:56 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Parsing-fields-from-json-logs/m-p/267999#M51428</guid>
      <dc:creator>splunk_svc</dc:creator>
      <dc:date>2016-10-27T04:10:56Z</dc:date>
    </item>
    <item>
      <title>Re: Parsing fields from json logs</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Parsing-fields-from-json-logs/m-p/268000#M51429</link>
      <description>&lt;P&gt;Sorry about the confusion.&lt;/P&gt;

&lt;P&gt;The trailing backslash is NOT coming in with the data.&lt;BR /&gt;
This was only displayed when I viewed the output in table mode.&lt;BR /&gt;
The table mode was escaping the quotes with a backslash.&lt;BR /&gt;
Viewing it in raw mode shows the field(s) to be quite delimited (only).&lt;/P&gt;</description>
      <pubDate>Thu, 27 Oct 2016 04:50:53 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Parsing-fields-from-json-logs/m-p/268000#M51429</guid>
      <dc:creator>splunk_svc</dc:creator>
      <dc:date>2016-10-27T04:50:53Z</dc:date>
    </item>
  </channel>
</rss>

