<?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 Using MVZip and MVExpand on MultiValue fields where one node sometimes exists in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Using-MVZip-and-MVExpand-on-MultiValue-fields-where-one-node/m-p/366396#M108071</link>
    <description>&lt;P&gt;Hello friendly Splunk community,&lt;/P&gt;

&lt;P&gt;May I ask your assistance in dealing with a multivalue field that sometimes contains one item and sometimes does not contain that item.&lt;/P&gt;

&lt;P&gt;For example - the JSON data looks like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;VerificationItems: [
  {
    Description: Description1   
  } 
  {
    Description: Description2
  } 
  {
    Description: Description3
    ErrorMessage: ErrorMessage3
  } 
]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Notice that the "ErrorMessage" field does not appear in every item.&lt;/P&gt;

&lt;P&gt;What I would LIKE the output to look like is:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Description    ErrorMessage
----------------------------
Description1   
Description2   
Description3   ErrorMessage3
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The problem is that the "ErrorMessage" field doesn't exist in every subitem of VerificationItems.&lt;/P&gt;

&lt;P&gt;I've attempted to use mvzip to combine all Descriptions into a single multivalue field, and do the same with all ErrorMessages, then recombine them using mvindex, as shown in the query below.  This works well if the "ErrorMessage" field exists in every subitem.  However, in my case it only appears when it has a value.  &lt;/P&gt;

&lt;P&gt;This is the query that almost works, but it mis-aligns the values:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;spath VerificationItems{}.Description | rename VerificationItems{}.Description AS D 
| spath VerificationItems{}.ErrorMessage | rename VerificationItems{}.ErrorMessage as E 
| eval x=mvzip(D, E, ";;") | mvexpand x 
| eval x=split(x,";;") 
| eval Descr=mvindex(x,0) 
| eval ErrorMessage=mvindex(x,1)
| table _time, Descr, ErrorMessage
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The current (and incorrect) end result is:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Description    ErrorMessage
----------------------------
Description1   ErrorMessage3   &amp;lt;-- ErrorMessage3 shows up incorrectly on the first line
Description2   
Description3   
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;...which you can see incorrectly aligns ErrorMessage3 with Description1.&lt;/P&gt;

&lt;P&gt;I've tried using regex with max_match=0 on the 'VerificationItems' multivalue field without success.  (I get zero results).  I also attempted a FillNull solution, which also doesn't work as I expected it to.&lt;/P&gt;

&lt;P&gt;Does the community have any suggestions on how to tweak this search to work correctly in the absence of that ErrorMessage field on every item?&lt;/P&gt;

&lt;P&gt;Thank you in advance for your consideration of assistance.&lt;/P&gt;</description>
    <pubDate>Fri, 10 Nov 2017 20:49:05 GMT</pubDate>
    <dc:creator>blairmd</dc:creator>
    <dc:date>2017-11-10T20:49:05Z</dc:date>
    <item>
      <title>Using MVZip and MVExpand on MultiValue fields where one node sometimes exists</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-MVZip-and-MVExpand-on-MultiValue-fields-where-one-node/m-p/366396#M108071</link>
      <description>&lt;P&gt;Hello friendly Splunk community,&lt;/P&gt;

&lt;P&gt;May I ask your assistance in dealing with a multivalue field that sometimes contains one item and sometimes does not contain that item.&lt;/P&gt;

&lt;P&gt;For example - the JSON data looks like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;VerificationItems: [
  {
    Description: Description1   
  } 
  {
    Description: Description2
  } 
  {
    Description: Description3
    ErrorMessage: ErrorMessage3
  } 
]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Notice that the "ErrorMessage" field does not appear in every item.&lt;/P&gt;

&lt;P&gt;What I would LIKE the output to look like is:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Description    ErrorMessage
----------------------------
Description1   
Description2   
Description3   ErrorMessage3
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The problem is that the "ErrorMessage" field doesn't exist in every subitem of VerificationItems.&lt;/P&gt;

&lt;P&gt;I've attempted to use mvzip to combine all Descriptions into a single multivalue field, and do the same with all ErrorMessages, then recombine them using mvindex, as shown in the query below.  This works well if the "ErrorMessage" field exists in every subitem.  However, in my case it only appears when it has a value.  &lt;/P&gt;

&lt;P&gt;This is the query that almost works, but it mis-aligns the values:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;spath VerificationItems{}.Description | rename VerificationItems{}.Description AS D 
| spath VerificationItems{}.ErrorMessage | rename VerificationItems{}.ErrorMessage as E 
| eval x=mvzip(D, E, ";;") | mvexpand x 
| eval x=split(x,";;") 
| eval Descr=mvindex(x,0) 
| eval ErrorMessage=mvindex(x,1)
| table _time, Descr, ErrorMessage
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The current (and incorrect) end result is:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Description    ErrorMessage
----------------------------
Description1   ErrorMessage3   &amp;lt;-- ErrorMessage3 shows up incorrectly on the first line
Description2   
Description3   
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;...which you can see incorrectly aligns ErrorMessage3 with Description1.&lt;/P&gt;

&lt;P&gt;I've tried using regex with max_match=0 on the 'VerificationItems' multivalue field without success.  (I get zero results).  I also attempted a FillNull solution, which also doesn't work as I expected it to.&lt;/P&gt;

&lt;P&gt;Does the community have any suggestions on how to tweak this search to work correctly in the absence of that ErrorMessage field on every item?&lt;/P&gt;

&lt;P&gt;Thank you in advance for your consideration of assistance.&lt;/P&gt;</description>
      <pubDate>Fri, 10 Nov 2017 20:49:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-MVZip-and-MVExpand-on-MultiValue-fields-where-one-node/m-p/366396#M108071</guid>
      <dc:creator>blairmd</dc:creator>
      <dc:date>2017-11-10T20:49:05Z</dc:date>
    </item>
    <item>
      <title>Re: Using MVZip and MVExpand on MultiValue fields where one node sometimes exists</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-MVZip-and-MVExpand-on-MultiValue-fields-where-one-node/m-p/366397#M108072</link>
      <description>&lt;P&gt;without dissecting the search too much, can you just create E if it doesn't exist after the spath and before the mvzip?&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;   | . . .
   | spath VerificationItems{}.ErrorMessage | rename VerificationItems{}.ErrorMessage as E 
   | eval E = coalesce(E,"")
   | eval x=mvzip(D, E, ";;") | mvexpand x | eval E = coalesce(E,"")
   | ..... 
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 10 Nov 2017 22:16:17 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-MVZip-and-MVExpand-on-MultiValue-fields-where-one-node/m-p/366397#M108072</guid>
      <dc:creator>maciep</dc:creator>
      <dc:date>2017-11-10T22:16:17Z</dc:date>
    </item>
    <item>
      <title>Re: Using MVZip and MVExpand on MultiValue fields where one node sometimes exists</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-MVZip-and-MVExpand-on-MultiValue-fields-where-one-node/m-p/366398#M108073</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;

&lt;P&gt;I'm not sure but I think data showing incorrect due to "ErrorMessage" field not available with all "Description" fields.&lt;/P&gt;

&lt;P&gt;I have tried with below sample json in which "ErrorMessage" field has BLANK (NO) value. In this case values extracted properly.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;{"VerificationItems": [{"Description": "Description1","ErrorMessage": ""},{"Description": "Description2","ErrorMessage": ""},{"Description": "Description3","ErrorMessage": "ErrorMessage3"} ]}
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I have tried with below search.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;YOUR_SEARCH
| spath VerificationItems{}.Description 
| rename VerificationItems{}.Description AS Description 
| spath VerificationItems{}.ErrorMessage 
| rename VerificationItems{}.ErrorMessage as ErrorMessage 
| eval temp = mvzip(Description,ErrorMessage) | stats count by _time temp
| eval Description = mvindex(split(temp,","),0), ErrorMessage = mvindex(split(temp,","),1) 
| table _time Description,ErrorMessage
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Is It possible to make a change in JSON data we received and add ErrorMessage BLANK when it is not available? Do you able to change and try above search?&lt;/P&gt;

&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Sat, 11 Nov 2017 17:45:44 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-MVZip-and-MVExpand-on-MultiValue-fields-where-one-node/m-p/366398#M108073</guid>
      <dc:creator>kamlesh_vaghela</dc:creator>
      <dc:date>2017-11-11T17:45:44Z</dc:date>
    </item>
    <item>
      <title>Re: Using MVZip and MVExpand on MultiValue fields where one node sometimes exists</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-MVZip-and-MVExpand-on-MultiValue-fields-where-one-node/m-p/366399#M108074</link>
      <description>&lt;P&gt;@blairmd, as @kamlesh_vaghela has mentioned, &lt;BR /&gt;
1) You should better fix empty values in JSON data being logged rather than at search time mocking dummy value (enrichment of data after stats command is better than cooking up _raw data). &lt;BR /&gt;
2) Also, you JSON does not seem to be formatted correctly in the question. Ideally Key Names and their corresponding String Values should be in double quotes and each KV pair should be comma separated.&lt;/P&gt;

&lt;P&gt;Following is a run anywhere search based on corrected JSON data based on what you have provided. Double quotes have been escaped to dummy required data. You would need to replace first two pipes i.e. &lt;CODE&gt;| makeresults&lt;/CODE&gt; and &lt;CODE&gt;|eval _raw&lt;/CODE&gt; with your actual base search.&lt;/P&gt;

&lt;P&gt;The first replace command is identifying nodes with only Description but no ErrorMessage (PS: In such situations the Description will be followed by end curly brace &lt;CODE&gt;}&lt;/CODE&gt; instead of comma ",". Replace uses regular expression to find such instances and introduce dummy node &lt;CODE&gt;, ErrorMessage: "NA"&lt;/CODE&gt;. PS: I have used simple regex &lt;CODE&gt;\w+&lt;/CODE&gt; for identifying Descriptions however it has to be corrected by you based on your actual description pattern in your data. Once dummy ErrorDescription &lt;CODE&gt;NA&lt;/CODE&gt; gets added your remaining search should work fine. I have added a final replace before displaying the field values of Description and ErrorMessage to convert &lt;CODE&gt;NA&lt;/CODE&gt; to &lt;CODE&gt;blank&lt;/CODE&gt; i.e. &lt;CODE&gt;""&lt;/CODE&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;|  makeresults
|  eval _raw="{ \"VerificationItems\": [
   {
     \"Description\": \"Description1\"    
   },
   {
     \"Description\": \"Description2\"
   },
   {
     \"Description\": \"Description3\",
     \"ErrorMessage\": \"ErrorMessage3\"
   }
 ]}
"
| eval _raw=replace(_raw,"(\"Description\":\s\"\w+\"\s+)(\})","\1,\"ErrorMessage\": \"NA\"\2")
| spath
| rename VerificationItems{}.Description AS D 
| rename VerificationItems{}.ErrorMessage AS E 
| eval x=mvzip(D, E, ";;") 
| mvexpand x 
| eval x=split(x,";;") 
| eval Description=mvindex(x,0) 
| eval ErrorMessage=mvindex(x,1)
| replace "NA" with "" in ErrorMessage
| table _time, Description, ErrorMessage
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Please try out and confirm. Use &lt;A href="https://regex101.com/"&gt;https://regex101.com/&lt;/A&gt; to test the Regular Expression to identify Description field and value.&lt;/P&gt;</description>
      <pubDate>Sat, 11 Nov 2017 18:45:40 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-MVZip-and-MVExpand-on-MultiValue-fields-where-one-node/m-p/366399#M108074</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2017-11-11T18:45:40Z</dc:date>
    </item>
    <item>
      <title>Re: Using MVZip and MVExpand on MultiValue fields where one node sometimes exists</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-MVZip-and-MVExpand-on-MultiValue-fields-where-one-node/m-p/366400#M108075</link>
      <description>&lt;P&gt;This is basically the approach I took.  I do not have the ability to correct the JSON format - that is being generated by the Application Developers, who prioritize any adjustment of log data to be very low.  So instead, I created a new regex to capture the value of any "ErrorMessage" node that exists alongside of the Description.  Thanks for your time on this.&lt;/P&gt;</description>
      <pubDate>Mon, 13 Nov 2017 12:49:00 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-MVZip-and-MVExpand-on-MultiValue-fields-where-one-node/m-p/366400#M108075</guid>
      <dc:creator>blairmd</dc:creator>
      <dc:date>2017-11-13T12:49:00Z</dc:date>
    </item>
  </channel>
</rss>

