<?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 Splunk not able to extract fields properly in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Splunk-not-able-to-extract-fields-properly/m-p/681809#M232977</link>
    <description>&lt;P&gt;Hi Splunk Experts,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have some data coming into splunk which has the following format:&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;[{"columns":[{"text":"id","type":"string"},{"text":"event","type":"number"},{"text":"delays","type":"number"},{"text":"drops","type":"number"}],"rows":[["BM0077",35602782,3043.01,0],["BM1604",2920978,4959.1,2],["BM1612",2141607,5623.3,6],["BM2870",41825122,2545.34,7],["BM1834",74963092,2409.0,8],["BM0267",86497692,1804.55,44],["BM059",1630092,5684.5,0]],"type":"table"}]&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I tried to extract each field so that each value&amp;nbsp; corresponds to id,event,delays and drops as a table using the below command.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;index=result  | rex field=_raw max_match=0 "\[\"(?&amp;lt;id&amp;gt;[^\"]+)\",\s*(?&amp;lt;event&amp;gt;\d+),\s*(?&amp;lt;delays&amp;gt;\d+\.\d+),\s*(?&amp;lt;drops&amp;gt;\d+)" | table id&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;event delays drops&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I get the result in table format , however it spits out as one whole table and not individual entries and I cannot manipulate the result.&amp;nbsp; I have tried using mvexpand , however it can only do for one value, so have not been helpful as well.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does anyone know how we can properly get the table in splunk .&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 25 Mar 2024 10:36:37 GMT</pubDate>
    <dc:creator>janesh222</dc:creator>
    <dc:date>2024-03-25T10:36:37Z</dc:date>
    <item>
      <title>Splunk not able to extract fields properly</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Splunk-not-able-to-extract-fields-properly/m-p/681809#M232977</link>
      <description>&lt;P&gt;Hi Splunk Experts,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have some data coming into splunk which has the following format:&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;[{"columns":[{"text":"id","type":"string"},{"text":"event","type":"number"},{"text":"delays","type":"number"},{"text":"drops","type":"number"}],"rows":[["BM0077",35602782,3043.01,0],["BM1604",2920978,4959.1,2],["BM1612",2141607,5623.3,6],["BM2870",41825122,2545.34,7],["BM1834",74963092,2409.0,8],["BM0267",86497692,1804.55,44],["BM059",1630092,5684.5,0]],"type":"table"}]&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I tried to extract each field so that each value&amp;nbsp; corresponds to id,event,delays and drops as a table using the below command.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;index=result  | rex field=_raw max_match=0 "\[\"(?&amp;lt;id&amp;gt;[^\"]+)\",\s*(?&amp;lt;event&amp;gt;\d+),\s*(?&amp;lt;delays&amp;gt;\d+\.\d+),\s*(?&amp;lt;drops&amp;gt;\d+)" | table id&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;event delays drops&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I get the result in table format , however it spits out as one whole table and not individual entries and I cannot manipulate the result.&amp;nbsp; I have tried using mvexpand , however it can only do for one value, so have not been helpful as well.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does anyone know how we can properly get the table in splunk .&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Mar 2024 10:36:37 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Splunk-not-able-to-extract-fields-properly/m-p/681809#M232977</guid>
      <dc:creator>janesh222</dc:creator>
      <dc:date>2024-03-25T10:36:37Z</dc:date>
    </item>
    <item>
      <title>Re: Splunk not able to extract fields properly</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Splunk-not-able-to-extract-fields-properly/m-p/681819#M232981</link>
      <description>&lt;P&gt;This is a task for &lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Spath" target="_blank" rel="noopener"&gt;spath&lt;/A&gt; and &lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/JSONFunctions" target="_blank" rel="noopener"&gt;JSON functions&lt;/A&gt;, not for rex. &amp;nbsp;Important: Do not treat structured data as strings. &amp;nbsp;But most importantly, if there is a worse data design in JSON than this, I haven't seen it before. (Trust me, I've seen many bad JSON designs.) &amp;nbsp;Your developers really go to lengths to show the world how lazy they can be. &amp;nbsp;After lengthy reverse engineering, I cannot decide whether this is the poorest transliteration of an existing data table into JSON, or the worst imagined construction of a data table in JSON. &amp;nbsp;Using two separate arrays to describe row and column saves a little bit of space, but unlike in a SQL database, this makes search less efficient. (Unless the application reads the whole thing and reconstruct data into an in-memory SQL database. &amp;nbsp;It also wastes text to describe data type in each column when you JSON format has enough datatypes to include strings and numbers.&lt;/P&gt;&lt;P&gt;Enough ranting. &amp;nbsp;If you have any influence over your developers, make them change the event format to something like this instead:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;[{"id":"BM0077","event":35602782,"delays":3043.01,"drops":0},{"id":"BM0267","event":86497692,"delays":1804.55,"drops":44},{"id":"BM059","event":1630092,"delays":5684.5,"drops":0},{"id":"BM1604","event":2920978,"delays":4959.1,"drops":2},{"id":"BM1612","event":2141607,"delays":5623.3,"drops":6},{"id":"BM1834","event":74963092,"delays":2409,"drops":8},{"id":"BM2870","event":41825122,"delays":2545.34,"drops":7}]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;With properly designed JSON data, extraction can be simple as&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;``` extract from well-designed JSON ```
| spath path={}
| mvexpand {}
| spath input={}
| fields - _* {}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Output will your sample data (see emulation below) will be&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;delays&lt;/TD&gt;&lt;TD&gt;drops&lt;/TD&gt;&lt;TD&gt;event&lt;/TD&gt;&lt;TD&gt;id&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3043.01&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;35602782&lt;/TD&gt;&lt;TD&gt;BM0077&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1804.55&lt;/TD&gt;&lt;TD&gt;44&lt;/TD&gt;&lt;TD&gt;86497692&lt;/TD&gt;&lt;TD&gt;BM0267&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;5684.5&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;1630092&lt;/TD&gt;&lt;TD&gt;BM059&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4959.1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2920978&lt;/TD&gt;&lt;TD&gt;BM1604&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;5623.3&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;2141607&lt;/TD&gt;&lt;TD&gt;BM1612&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2409&lt;/TD&gt;&lt;TD&gt;8&lt;/TD&gt;&lt;TD&gt;74963092&lt;/TD&gt;&lt;TD&gt;BM1834&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2545.34&lt;/TD&gt;&lt;TD&gt;7&lt;/TD&gt;&lt;TD&gt;41825122&lt;/TD&gt;&lt;TD&gt;BM2870&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;This is how to reach good JSON structure from the sample's bad structure:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;``` transform from bad JSON to well-designed JSON ```
| spath path={}
| mvexpand {}
| fromjson {}
| mvexpand rows
| eval idx = mvrange(0, mvcount(columns))
| eval data = json_object()
| foreach idx mode=multivalue
    [eval row = mvindex(json_array_to_mv(rows), &amp;lt;&amp;lt;ITEM&amp;gt;&amp;gt;), data = json_set(data, json_extract(mvindex(columns, &amp;lt;&amp;lt;ITEM&amp;gt;&amp;gt;), "text"), row)]
| stats values(data) as _raw
| eval _raw = mv_to_json_array(_raw, true())&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But before your developer can change their mind, you can do one of the following.&lt;/P&gt;&lt;P&gt;1. String together the bad-JSON-to-good-JSON transformation and normal extraction&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;``` transform, then extract ```
``` transform from bad JSON to well-designed JSON ```
| spath path={}
| mvexpand {}
| fromjson {}
| mvexpand rows
| eval idx = mvrange(0, mvcount(columns))
| eval data = json_object()
| foreach idx mode=multivalue
    [eval row = mvindex(json_array_to_mv(rows), &amp;lt;&amp;lt;ITEM&amp;gt;&amp;gt;), data = json_set(data, json_extract(mvindex(columns, &amp;lt;&amp;lt;ITEM&amp;gt;&amp;gt;), "text"), row)]
| stats values(data) as _raw
| eval _raw = mv_to_json_array(_raw, true())
``` extract from well-designed JSON ```
| spath path={}
| mvexpand {}
| spath input={}
| fields - _* {}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2. Transform the poorly designed JSON into table format with a little help from &lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Extract" target="_blank" rel="noopener"&gt;kv&lt;/A&gt; aka extract, like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;``` directly handle bad design ```
| spath path={}
| mvexpand {}
| fromjson {}
| fields - _* {} type
| mvexpand rows
| eval idx = mvrange(0, mvcount(columns))
``` the above is the same as transformation ```
| foreach idx mode=multivalue
    [eval _raw = mvappend(_raw, json_extract(mvindex(columns, &amp;lt;&amp;lt;ITEM&amp;gt;&amp;gt;), "text") . "=" . mvindex(json_array_to_mv(rows), &amp;lt;&amp;lt;ITEM&amp;gt;&amp;gt;))]
| fields - rows columns idx
| extract&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This will give you the same output as above.&lt;/P&gt;&lt;P&gt;Here is a data emulation you can play with the above methods and compare with real data&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults
| eval _raw = "[{\"columns\":[{\"text\":\"id\",\"type\":\"string\"},{\"text\":\"event\",\"type\":\"number\"},{\"text\":\"delays\",\"type\":\"number\"},{\"text\":\"drops\",\"type\":\"number\"}],\"rows\":[[\"BM0077\",35602782,3043.01,0],[\"BM1604\",2920978,4959.1,2],[\"BM1612\",2141607,5623.3,6],[\"BM2870\",41825122,2545.34,7],[\"BM1834\",74963092,2409.0,8],[\"BM0267\",86497692,1804.55,44],[\"BM059\",1630092,5684.5,0]],\"type\":\"table\"}]"
``` data emulation above ```&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A final note: spath has some difficulty handling array of arrays, so I used &lt;A href="xhttps://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Fromjson" target="_blank" rel="noopener"&gt;fromjson&lt;/A&gt; (available since 9.0) in one filter.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Mar 2024 08:30:57 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Splunk-not-able-to-extract-fields-properly/m-p/681819#M232981</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2024-03-25T08:30:57Z</dc:date>
    </item>
    <item>
      <title>Re: Splunk not able to extract fields properly</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Splunk-not-able-to-extract-fields-properly/m-p/681943#M233016</link>
      <description>&lt;P&gt;Thanks a lot yuanli,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That worked , you are a genuis. I thought I could never structure it in splunk.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I did speak to the dev team , apparently the json is structured in that way to feed a particular dashboard system that they use. So it has to be in that structure for that system to consume. However they have agreed to update the structure in the next release which could be a few months away (6 months atleast) . So in the mean time I could work with this bad json until then.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&amp;nbsp; a lot again. I had searched in splunk for something like this before and havent seen anything.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Mar 2024 03:50:03 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Splunk-not-able-to-extract-fields-properly/m-p/681943#M233016</guid>
      <dc:creator>janesh222</dc:creator>
      <dc:date>2024-03-26T03:50:03Z</dc:date>
    </item>
  </channel>
</rss>

