<?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: Part 1: How to extract a json portion of an event then use spath to extract key=value pairs in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58895#M14478</link>
    <description>&lt;P&gt;This approach should work. However, I am still unable to make the regex work in my environment.&lt;/P&gt;</description>
    <pubDate>Thu, 21 Mar 2013 14:27:47 GMT</pubDate>
    <dc:creator>lpolo</dc:creator>
    <dc:date>2013-03-21T14:27:47Z</dc:date>
    <item>
      <title>Part 1: How to extract a json portion of an event then use spath to extract key=value pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58880#M14463</link>
      <description>&lt;P&gt;I have the following log event but I have not been able to use spath to extract the json key=value pairs. &lt;/P&gt;

&lt;P&gt;2013-03-12 10:37:10,205 &amp;lt;tvsquery id=58b6bf4d-948b-416b-8d17-cedcbc1059ec&amp;gt;&lt;STRONG&gt;&lt;EM&gt;{&lt;BR /&gt;
  "start" : 1,&lt;BR /&gt;
  "returned" : 0,&lt;BR /&gt;
  "count" : 0&lt;BR /&gt;
}&lt;/EM&gt;&lt;/STRONG&gt;&amp;lt;/tvsquery&amp;gt;&lt;/P&gt;

&lt;P&gt;Therefore, I tried to extract the json portion with this regex and then use spath:&lt;/P&gt;

&lt;P&gt;|rex field=_raw "&lt;TVSQUERY id="(?&amp;lt;id"&gt;[^&amp;gt;]+)&amp;gt;(?&lt;RESPONSE&gt;.+?)&lt;/RESPONSE&gt;"|spath input=response&lt;/TVSQUERY&gt;&lt;/P&gt;

&lt;P&gt;But I having a hard time to make it work.&lt;/P&gt;

&lt;P&gt;How can I extract the json portion of the event and then use spath to extract the key=value pairs?&lt;/P&gt;

&lt;P&gt;Thanks,&lt;BR /&gt;
Lp&lt;/P&gt;</description>
      <pubDate>Tue, 12 Mar 2013 14:15:11 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58880#M14463</guid>
      <dc:creator>lpolo</dc:creator>
      <dc:date>2013-03-12T14:15:11Z</dc:date>
    </item>
    <item>
      <title>Re: Part 1: How to extract a json portion of an event then use spath to extract key=value pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58881#M14464</link>
      <description>&lt;P&gt;I believe you need to keep the {} in the field as well, which your rex omits.  Try this for your rex:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;rex "(?&amp;lt; json_field&amp;gt;{[^}]+})"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;(remove the space used to keep text from disappearing)&lt;/P&gt;

&lt;P&gt;Or if you want to keep the more complex regex, simply move the closing } into the capture group.&lt;/P&gt;

&lt;P&gt;You should then be able to use spath on the rex'ed field.  &lt;/P&gt;

&lt;P&gt;EDIT TO ADD:&lt;BR /&gt;
If you've got events with multiple JSON objects, then you'll have to do a bit more work with it.  I would recommend adding the max_match param to the rex, which will find multiple matches and collect them into a multi-valued field.  Then you can mvexpand that field to multiple events, and parse that with spath.  &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;rex max_match=10 "(?&amp;lt; json_field&amp;gt;{[^}]+})" | mvexpand json_field | spath input=json_field ...
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;(replace rex as needed)&lt;/P&gt;</description>
      <pubDate>Tue, 12 Mar 2013 14:53:48 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58881#M14464</guid>
      <dc:creator>emiller42</dc:creator>
      <dc:date>2013-03-12T14:53:48Z</dc:date>
    </item>
    <item>
      <title>Re: Part 1: How to extract a json portion of an event then use spath to extract key=value pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58882#M14465</link>
      <description>&lt;P&gt;is that json &lt;EM&gt;in&lt;/EM&gt; xml  ? yowsers&lt;/P&gt;</description>
      <pubDate>Tue, 12 Mar 2013 14:55:20 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58882#M14465</guid>
      <dc:creator>jonuwz</dc:creator>
      <dc:date>2013-03-12T14:55:20Z</dc:date>
    </item>
    <item>
      <title>Re: Part 1: How to extract a json portion of an event then use spath to extract key=value pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58883#M14466</link>
      <description>&lt;P&gt;Thanks, It worked.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Mar 2013 15:03:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58883#M14466</guid>
      <dc:creator>lpolo</dc:creator>
      <dc:date>2013-03-12T15:03:41Z</dc:date>
    </item>
    <item>
      <title>Re: Part 1: How to extract a json portion of an event then use spath to extract key=value pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58884#M14467</link>
      <description>&lt;P&gt;I tried using the regex in a more complex json field but it fails. It worked for the simple json presented in the example.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Mar 2013 15:16:58 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58884#M14467</guid>
      <dc:creator>lpolo</dc:creator>
      <dc:date>2013-03-12T15:16:58Z</dc:date>
    </item>
    <item>
      <title>Re: Part 1: How to extract a json portion of an event then use spath to extract key=value pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58885#M14468</link>
      <description>&lt;P&gt;With difficulty.&lt;/P&gt;

&lt;P&gt;Here's a starter :&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;...
| rex "&amp;lt;tvsquery id=(?&amp;lt;id&amp;gt;[^&amp;gt;]+)&amp;gt;(?&amp;lt;response&amp;gt;.*)&amp;lt;/tvsquery&amp;gt;" 
| table id response _time
| spath input=response
| eval key=_time.";".id
| fields - response _time id
| untable key field value
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Now you can either have the values in seperate events :&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| rex field=key "(?&amp;lt;_time&amp;gt;.*);(?&amp;lt;id&amp;gt;.*)"
| fields - key
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Or for easy reading :&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| stats list(field) as fields list(value) as values by key 
| rex field=key "(?&amp;lt;_time&amp;gt;.*);(?&amp;lt;id&amp;gt;.*)"
| fields - key
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Mar 2013 15:19:38 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58885#M14468</guid>
      <dc:creator>jonuwz</dc:creator>
      <dc:date>2013-03-12T15:19:38Z</dc:date>
    </item>
    <item>
      <title>Re: Part 1: How to extract a json portion of an event then use spath to extract key=value pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58886#M14469</link>
      <description>&lt;P&gt;I think the most important thing is that in your original rex, the closing } wasn't part of the capture group, so the field being extracted was &lt;BR /&gt;
    { "start" : 1, "returned" : 0, "count" : 0&lt;BR /&gt;
which spath will fail on.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Mar 2013 15:53:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58886#M14469</guid>
      <dc:creator>emiller42</dc:creator>
      <dc:date>2013-03-12T15:53:42Z</dc:date>
    </item>
    <item>
      <title>Re: Part 1: How to extract a json portion of an event then use spath to extract key=value pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58887#M14470</link>
      <description>&lt;P&gt;Thanks for the observation. I corrected this problem as you recommended. And I was able to extract the json portion of the event and use spath. However, I am facing the same issue I had at the beginning: if the extracted json field contains multiple arrays and objects both regex fail to extract json portion of the event.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Mar 2013 17:13:06 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58887#M14470</guid>
      <dc:creator>lpolo</dc:creator>
      <dc:date>2013-03-12T17:13:06Z</dc:date>
    </item>
    <item>
      <title>Re: Part 1: How to extract a json portion of an event then use spath to extract key=value pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58888#M14471</link>
      <description>&lt;P&gt;what do you recommend?&lt;/P&gt;</description>
      <pubDate>Tue, 12 Mar 2013 17:17:59 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58888#M14471</guid>
      <dc:creator>lpolo</dc:creator>
      <dc:date>2013-03-12T17:17:59Z</dc:date>
    </item>
    <item>
      <title>Re: Part 1: How to extract a json portion of an event then use spath to extract key=value pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58889#M14472</link>
      <description>&lt;P&gt;Then I would add a max_match= condition to the rex, so it could capture more than one JSON array into a multi-valued field.  Then pipe that to mvexpand so that they get split to multiple events.&lt;BR /&gt;&lt;BR /&gt;
    rex max_match=10 "regex_string" |mvexpand field_name | spath ...&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 13:30:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58889#M14472</guid>
      <dc:creator>emiller42</dc:creator>
      <dc:date>2020-09-28T13:30:12Z</dc:date>
    </item>
    <item>
      <title>Re: Part 1: How to extract a json portion of an event then use spath to extract key=value pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58890#M14473</link>
      <description>&lt;P&gt;It did not work. It continues to fail if the extracted json field contains multiple arrays and objects. I made sure that max_match=value was not greater that the number of objects.&lt;BR /&gt;
Could you kindly see the log example I posted in case 114699?&lt;BR /&gt;
thanks,LP&lt;/P&gt;</description>
      <pubDate>Tue, 12 Mar 2013 17:44:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58890#M14473</guid>
      <dc:creator>lpolo</dc:creator>
      <dc:date>2013-03-12T17:44:46Z</dc:date>
    </item>
    <item>
      <title>Re: Part 1: How to extract a json portion of an event then use spath to extract key=value pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58891#M14474</link>
      <description>&lt;P&gt;I'm just another user, so I can't see your cases.  Sorry!&lt;/P&gt;</description>
      <pubDate>Tue, 12 Mar 2013 17:54:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58891#M14474</guid>
      <dc:creator>emiller42</dc:creator>
      <dc:date>2013-03-12T17:54:28Z</dc:date>
    </item>
    <item>
      <title>Re: Part 1: How to extract a json portion of an event then use spath to extract key=value pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58892#M14475</link>
      <description>&lt;P&gt;I reviewed my props.conf and I removed&lt;BR /&gt;
KV_MODE = json from the related sourcetype. Then, reload props.conf and I ran the query with your recommendation. It seems to be working. I need to validate the result set.&lt;/P&gt;

&lt;P&gt;Thanks,&lt;BR /&gt;
Lp&lt;/P&gt;</description>
      <pubDate>Tue, 12 Mar 2013 18:10:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58892#M14475</guid>
      <dc:creator>lpolo</dc:creator>
      <dc:date>2013-03-12T18:10:18Z</dc:date>
    </item>
    <item>
      <title>Re: Part 1: How to extract a json portion of an event then use spath to extract key=value pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58893#M14476</link>
      <description>&lt;P&gt;If this is working, would you mind accepting the answer?  That way other people searching will see this is something with a resolution.  Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 14 Mar 2013 17:50:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58893#M14476</guid>
      <dc:creator>emiller42</dc:creator>
      <dc:date>2013-03-14T17:50:04Z</dc:date>
    </item>
    <item>
      <title>Re: Part 1: How to extract a json portion of an event then use spath to extract key=value pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58894#M14477</link>
      <description>&lt;P&gt;The regular expression behaves if an only if there is not any json array like the presented example. &lt;BR /&gt;
It partially solves the problem.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Mar 2013 15:17:06 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58894#M14477</guid>
      <dc:creator>lpolo</dc:creator>
      <dc:date>2013-03-20T15:17:06Z</dc:date>
    </item>
    <item>
      <title>Re: Part 1: How to extract a json portion of an event then use spath to extract key=value pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58895#M14478</link>
      <description>&lt;P&gt;This approach should work. However, I am still unable to make the regex work in my environment.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Mar 2013 14:27:47 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58895#M14478</guid>
      <dc:creator>lpolo</dc:creator>
      <dc:date>2013-03-21T14:27:47Z</dc:date>
    </item>
    <item>
      <title>Re: Part 1: How to extract a json portion of an event then use spath to extract key=value pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58896#M14479</link>
      <description>&lt;P&gt;Why not ?  what does response get populated with ? (just run the 1st 2 lines)&lt;/P&gt;</description>
      <pubDate>Thu, 21 Mar 2013 16:03:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58896#M14479</guid>
      <dc:creator>jonuwz</dc:creator>
      <dc:date>2013-03-21T16:03:46Z</dc:date>
    </item>
    <item>
      <title>Re: Part 1: How to extract a json portion of an event then use spath to extract key=value pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58897#M14480</link>
      <description>&lt;P&gt;The following regex will work, if and only if, there is not any new line in the event:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;rex "&amp;lt;tvsquery id=(?&amp;lt;id&amp;gt;[^&amp;gt;]+)&amp;gt;(?&amp;lt;response&amp;gt;.+?)&amp;lt;/tvsquery&amp;gt;"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Therefore, I was able to make it work by trimming the event before the regular expression as follow:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| rex field=_raw mode=sed "s/[\r\n]//g" 
| rex "&amp;lt;tvsquery id=(?&amp;lt;id&amp;gt;[^&amp;gt;]+)&amp;gt;(?&amp;lt;response&amp;gt;.+?)&amp;lt;/tvsquery&amp;gt;" 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Then, the extracted field "response" can be processed by spath search command.&lt;/P&gt;

&lt;P&gt;Regards,&lt;BR /&gt;
Lp&lt;/P&gt;</description>
      <pubDate>Thu, 28 Mar 2013 12:24:56 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58897#M14480</guid>
      <dc:creator>lpolo</dc:creator>
      <dc:date>2013-03-28T12:24:56Z</dc:date>
    </item>
    <item>
      <title>Re: Part 1: How to extract a json portion of an event then use spath to extract key=value pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58898#M14481</link>
      <description>&lt;P&gt;By an unknown reason, I have to replace single quotes for double quotes in order to make a duplicate spath call.&lt;BR /&gt;
"level" is a JSON field inside a JSON in message field.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; * | spath message | eval message=replace(message,"'","\"") |spath input=message | search level=INFO
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Got the tip from here &lt;A href="https://answers.splunk.com/answers/444133/extract-json-from-a-field.html"&gt;https://answers.splunk.com/answers/444133/extract-json-from-a-field.html&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Sep 2016 07:35:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58898#M14481</guid>
      <dc:creator>marciogh</dc:creator>
      <dc:date>2016-09-13T07:35:41Z</dc:date>
    </item>
    <item>
      <title>Re: Part 1: How to extract a json portion of an event then use spath to extract key=value pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58899#M14482</link>
      <description>&lt;P&gt;This is because using single-quotes isn't valid JSON, so it can't parse it as JSON.  &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;{"aaa": 1, "bbb": "some value"}
vs.
{'aaa': 1, 'bbb': 'some value'}
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The first is JSON.  The second is not.&lt;/P&gt;

&lt;P&gt;Since it's a field extracted from a larger JSON, I'm going to assume it's just incorrectly constructed.  Something like this would work fine, and not require multiple spath calls:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;{"id": 12345, "message": {"level": "INFO", "content": "foo bar baz"}}
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;But I'm going to guess what you have to work with is:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;{"id": 12345, "message": "{'level': 'INFO', 'content': 'foo bar baz'}"}
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Which isn't valid JSON at all.  (or more specifically, &lt;CODE&gt;message&lt;/CODE&gt; is just a string, and not a JSON object)&lt;/P&gt;</description>
      <pubDate>Wed, 14 Sep 2016 14:31:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Part-1-How-to-extract-a-json-portion-of-an-event-then-use-spath/m-p/58899#M14482</guid>
      <dc:creator>emiller42</dc:creator>
      <dc:date>2016-09-14T14:31:52Z</dc:date>
    </item>
  </channel>
</rss>

