<?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 How do I extract key values from a json object? in All Apps and Add-ons</title>
    <link>https://community.splunk.com/t5/All-Apps-and-Add-ons/How-do-I-extract-key-values-from-a-json-object/m-p/681075#M80368</link>
    <description>&lt;P&gt;Consider I have multiple such JSON events pushed to splunk.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;{
 "orderNum" : "1234",
 "orderLocation" : "demoLoc",
 "details":{
  "key1" : "value1",
  "key2" : "value2"
 }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to figure out a spunk query that would give me the following output in a table&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1" width="100%"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="25%" height="25px"&gt;orderNum&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;key&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;value&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;orderLocation&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="25%" height="25px"&gt;1234&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;key1&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;value1&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;demoLoc&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="25%" height="25px"&gt;1234&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;key2&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;value2&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;demoLoc&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;&lt;STRONG&gt;the value from the key-value pair can be an escaped JSON string. we also need to consider this while writing regex.&lt;/STRONG&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 26 Mar 2024 18:08:55 GMT</pubDate>
    <dc:creator>adikrhd</dc:creator>
    <dc:date>2024-03-26T18:08:55Z</dc:date>
    <item>
      <title>How do I extract key values from a json object?</title>
      <link>https://community.splunk.com/t5/All-Apps-and-Add-ons/How-do-I-extract-key-values-from-a-json-object/m-p/681075#M80368</link>
      <description>&lt;P&gt;Consider I have multiple such JSON events pushed to splunk.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;{
 "orderNum" : "1234",
 "orderLocation" : "demoLoc",
 "details":{
  "key1" : "value1",
  "key2" : "value2"
 }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to figure out a spunk query that would give me the following output in a table&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1" width="100%"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="25%" height="25px"&gt;orderNum&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;key&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;value&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;orderLocation&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="25%" height="25px"&gt;1234&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;key1&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;value1&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;demoLoc&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="25%" height="25px"&gt;1234&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;key2&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;value2&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;demoLoc&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;&lt;STRONG&gt;the value from the key-value pair can be an escaped JSON string. we also need to consider this while writing regex.&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Mar 2024 18:08:55 GMT</pubDate>
      <guid>https://community.splunk.com/t5/All-Apps-and-Add-ons/How-do-I-extract-key-values-from-a-json-object/m-p/681075#M80368</guid>
      <dc:creator>adikrhd</dc:creator>
      <dc:date>2024-03-26T18:08:55Z</dc:date>
    </item>
    <item>
      <title>Re: How do I extract key values from a json object?</title>
      <link>https://community.splunk.com/t5/All-Apps-and-Add-ons/How-do-I-extract-key-values-from-a-json-object/m-p/681093#M80369</link>
      <description>&lt;P&gt;This is kind of sed-y but it should work: (assuming your automatic kv field extraction is working on your json event)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| spath input=_raw path=details output=hold
| rex field=hold mode=sed "s/({\s*|\s*}|,\s*)//g"
| makemv hold delim="\"\""
| mvexpand hold
| rex field=hold "(?&amp;lt;key&amp;gt;[^,\s\"]*)\"\s:\s\"(?&amp;lt;value&amp;gt;[^,\s\"]*)" max_match=0
| table orderNum key value orderLocation&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Mar 2024 20:23:55 GMT</pubDate>
      <guid>https://community.splunk.com/t5/All-Apps-and-Add-ons/How-do-I-extract-key-values-from-a-json-object/m-p/681093#M80369</guid>
      <dc:creator>marnall</dc:creator>
      <dc:date>2024-03-18T20:23:55Z</dc:date>
    </item>
    <item>
      <title>Re: How do I extract key values from a json object?</title>
      <link>https://community.splunk.com/t5/All-Apps-and-Add-ons/How-do-I-extract-key-values-from-a-json-object/m-p/681134#M80373</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/263556"&gt;@marnall&lt;/a&gt;,&lt;BR /&gt;I appreciate your response, it did work for me after tuning it a bit&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| spath input=_raw path=details output=hold
| rex field=hold mode=sed "s/({\s*|\s*}|\s*)//g"
| makemv hold delim=","
| mvexpand hold
| rex field=hold "\"(?&amp;lt;key&amp;gt;[^\"]+)\":\"(?&amp;lt;value&amp;gt;[^\"]+)\""
| table orderNum key value orderLocation&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;However, I have a follow-up on this.&lt;BR /&gt;our admin has set some limitations for mvexpand command when I try to increase the search period for the last 3 days,&lt;BR /&gt;I get this warning:&amp;nbsp;&lt;BR /&gt;&lt;STRONG&gt;output will be truncated at 6300 results due to excessive memory usage. Memory threshold of 500MB as configured in limits.conf / [mvexpand] / max_mem_usage_mb has been reached.&lt;BR /&gt;&lt;BR /&gt;&lt;/STRONG&gt;Is there any alternate way to achieve the same results without using mvexpand, considering that on average there can be more than 50-60 key-value present under the "details" of a single event, and there can be 40K events per 30 days (assuming the retention period of the events is 30 days)?&lt;/P&gt;</description>
      <pubDate>Tue, 19 Mar 2024 07:10:08 GMT</pubDate>
      <guid>https://community.splunk.com/t5/All-Apps-and-Add-ons/How-do-I-extract-key-values-from-a-json-object/m-p/681134#M80373</guid>
      <dc:creator>adikrhd</dc:creator>
      <dc:date>2024-03-19T07:10:08Z</dc:date>
    </item>
    <item>
      <title>Re: How do I extract key values from a json object?</title>
      <link>https://community.splunk.com/t5/All-Apps-and-Add-ons/How-do-I-extract-key-values-from-a-json-object/m-p/681242#M80377</link>
      <description>&lt;P&gt;Does this work better?&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| spath input=_raw path=details output=hold
| rex field=hold "\"(?&amp;lt;kvs&amp;gt;[^\"]*\"*[^\"]*\"*[^\"]*\"*)\"" max_match=0
| stats values(*) as * by kvs
| rex field=kvs "(?&amp;lt;key&amp;gt;[^\"]*)\" : \"(?&amp;lt;value&amp;gt;[^\"]*)" max_match=0
| table orderNum key value orderLocation&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 19 Mar 2024 19:46:24 GMT</pubDate>
      <guid>https://community.splunk.com/t5/All-Apps-and-Add-ons/How-do-I-extract-key-values-from-a-json-object/m-p/681242#M80377</guid>
      <dc:creator>marnall</dc:creator>
      <dc:date>2024-03-19T19:46:24Z</dc:date>
    </item>
    <item>
      <title>Re: How do I extract key values from a json object?</title>
      <link>https://community.splunk.com/t5/All-Apps-and-Add-ons/How-do-I-extract-key-values-from-a-json-object/m-p/682035#M80394</link>
      <description>&lt;P&gt;did not work,&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults 
| eval _raw = "{\"orderNum\":\"1234\",\"orderLocation\":\"demoLoc\",\"details\":{\"key1\":\"value1\",\"key2\":\"value2\"}}"
| spath
| spath input=_raw path=details output=hold
| rex field=hold "\"(?&amp;lt;kvs&amp;gt;[^\"]*\"*[^\"]*\"*[^\"]*\"*)\"" max_match=0
| stats values(*) as * by kvs
| rex field=kvs "(?&amp;lt;key&amp;gt;[^\"]*)\" : \"(?&amp;lt;value&amp;gt;[^\"]*)" max_match=0
| table orderNum key value orderLocation&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the value from the key-value pair can be an escaped JSON string. we also need to consider this while writing regex.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Mar 2024 18:08:14 GMT</pubDate>
      <guid>https://community.splunk.com/t5/All-Apps-and-Add-ons/How-do-I-extract-key-values-from-a-json-object/m-p/682035#M80394</guid>
      <dc:creator>adikrhd</dc:creator>
      <dc:date>2024-03-26T18:08:14Z</dc:date>
    </item>
    <item>
      <title>Re: How do I extract key values from a json object?</title>
      <link>https://community.splunk.com/t5/All-Apps-and-Add-ons/How-do-I-extract-key-values-from-a-json-object/m-p/684151#M80469</link>
      <description>&lt;P&gt;Small tweak to the regex: (removing two space characters from the second-to-last line)&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults 
| eval _raw = "{\"orderNum\":\"1234\",\"orderLocation\":\"demoLoc\",\"details\":{\"key1\":\"value1\",\"key2\":\"value2\"}}"
| spath
| spath input=_raw path=details output=hold
| rex field=hold "\"(?&amp;lt;kvs&amp;gt;[^\"]*\"*[^\"]*\"*[^\"]*\"*)\"" max_match=0
| stats values(*) as * by kvs
| rex field=kvs "(?&amp;lt;key&amp;gt;[^\"]*)\":\"(?&amp;lt;value&amp;gt;[^\"]*)" max_match=0
| table orderNum key value orderLocation&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp; If the value can be an escaped JSON string, then indeed you need to be more crafty with the regex. E.g.:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults 
| eval _raw = "{\"orderNum\":\"1234\",\"orderLocation\":\"demoLoc\",\"details\":{\"key1\":\"{\\\"jsonvalue\\\":\\\"jsonvaluevalue\\\",\\\"jsonvalue2\\\":\\\"jsonvaluevalue2\\\"}\",\"key2\":\"value2\"}}"
| spath
| spath input=_raw path=details output=hold
| rex field=hold "(?&amp;lt;kvs&amp;gt;\"[^\"]*\":\"{?[^}]*}?\")" max_match=0
| stats values(*) as * by kvs
| rex field=kvs "(?&amp;lt;key&amp;gt;[^\"]*)\":\"(?&amp;lt;value&amp;gt;{?[^{}]*}?)\"" max_match=0
| table orderNum key value orderLocation&lt;/LI-CODE&gt;</description>
      <pubDate>Sat, 13 Apr 2024 08:15:44 GMT</pubDate>
      <guid>https://community.splunk.com/t5/All-Apps-and-Add-ons/How-do-I-extract-key-values-from-a-json-object/m-p/684151#M80469</guid>
      <dc:creator>marnall</dc:creator>
      <dc:date>2024-04-13T08:15:44Z</dc:date>
    </item>
  </channel>
</rss>

