<?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 you use IF / Else statement to pull different fields including JSON elements? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-do-you-use-IF-Else-statement-to-pull-different-fields/m-p/540138#M152782</link>
    <description>&lt;P&gt;Hello Community,&lt;/P&gt;&lt;P&gt;2 part question: First, how to use an IF / ELSE statement, secondly, how to specify the JSON elements in the query. Any examples or helpful&lt;/P&gt;&lt;P&gt;How would I do a search query that depending on the log source, pulls different fields?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example&lt;/P&gt;&lt;P&gt;index=myIndex&lt;/P&gt;&lt;P&gt;| IF (source=Source1 OR sourcetype=sourceTypeB) pull JSON element1, element2, etc&lt;/P&gt;&lt;P&gt;| ELSE IF logSource=logSource2 pull fieldsname1, fieldname2, etc&lt;/P&gt;</description>
    <pubDate>Tue, 16 Feb 2021 16:34:44 GMT</pubDate>
    <dc:creator>ShoeBuster</dc:creator>
    <dc:date>2021-02-16T16:34:44Z</dc:date>
    <item>
      <title>How do you use IF / Else statement to pull different fields including JSON elements?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-you-use-IF-Else-statement-to-pull-different-fields/m-p/540138#M152782</link>
      <description>&lt;P&gt;Hello Community,&lt;/P&gt;&lt;P&gt;2 part question: First, how to use an IF / ELSE statement, secondly, how to specify the JSON elements in the query. Any examples or helpful&lt;/P&gt;&lt;P&gt;How would I do a search query that depending on the log source, pulls different fields?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example&lt;/P&gt;&lt;P&gt;index=myIndex&lt;/P&gt;&lt;P&gt;| IF (source=Source1 OR sourcetype=sourceTypeB) pull JSON element1, element2, etc&lt;/P&gt;&lt;P&gt;| ELSE IF logSource=logSource2 pull fieldsname1, fieldname2, etc&lt;/P&gt;</description>
      <pubDate>Tue, 16 Feb 2021 16:34:44 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-you-use-IF-Else-statement-to-pull-different-fields/m-p/540138#M152782</guid>
      <dc:creator>ShoeBuster</dc:creator>
      <dc:date>2021-02-16T16:34:44Z</dc:date>
    </item>
    <item>
      <title>Re: How do you use IF / Else statement to pull different fields including JSON elements?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-you-use-IF-Else-statement-to-pull-different-fields/m-p/540155#M152786</link>
      <description>&lt;P&gt;Splunk does not have an IF/ELSE statement.&amp;nbsp; It does, however, have&amp;nbsp;&lt;FONT face="courier new,courier"&gt;if&lt;/FONT&gt;&amp;nbsp;and &lt;FONT face="courier new,courier"&gt;case&lt;/FONT&gt; functions that can be used in the &lt;FONT face="courier new,courier"&gt;eval&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;where&lt;/FONT&gt; commands.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=myIndex
| eval foo = if(source=Source1 OR sourcetype=sourceTypeB), JSON element1, 
if(logSource=logSource2), fieldsname1, NULL), NULL)
| eval bar = case(source=Source1 OR sourcetype=sourceTypeB), JSON element2, logSource=logSource2, fieldname2, 1==1, NULL)
| ...&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 16 Feb 2021 19:28:44 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-you-use-IF-Else-statement-to-pull-different-fields/m-p/540155#M152786</guid>
      <dc:creator>richgalloway</dc:creator>
      <dc:date>2021-02-16T19:28:44Z</dc:date>
    </item>
    <item>
      <title>Re: How do you use IF / Else statement to pull different fields including JSON elements?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-you-use-IF-Else-statement-to-pull-different-fields/m-p/540162#M152788</link>
      <description>&lt;P&gt;Re: "how to specify the JSON elements in the query":&lt;/P&gt;&lt;P&gt;Example:&lt;/P&gt;&lt;P&gt;- if you have a field titled field1 that has JSON formatted data (e.g. {"att1":"val1","att2":"val2"}&lt;/P&gt;&lt;P&gt;You can do the following:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=myIndex 
| spath input=field1 ouput=att1 path=att1
| spath input=field1 output=att1 path=att2
| eval test=case(source=Source1 OR sourcetype=sourceTypeB, att1,logSource=logSource2,fieldname1) &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can also use&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| spath input=field1&lt;/LI-CODE&gt;&lt;P&gt;without any output or path if you want to extract all JSON fields. For more details on the spath command go here: &lt;A href="https://docs.splunk.com/Documentation/Splunk/8.1.2/SearchReference/Spath" target="_blank"&gt;https://docs.splunk.com/Documentation/Splunk/8.1.2/SearchReference/Spath&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Feb 2021 20:54:57 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-you-use-IF-Else-statement-to-pull-different-fields/m-p/540162#M152788</guid>
      <dc:creator>ericjorgensenjr</dc:creator>
      <dc:date>2021-02-16T20:54:57Z</dc:date>
    </item>
  </channel>
</rss>

