<?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: How to extract fields from JSON data based on API calls? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-extract-fields-from-JSON-data-based-on-API-calls/m-p/264576#M79412</link>
    <description>&lt;P&gt;If you JSON events have already been extracted correctly then you just need the following bits:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; your search query here
 | fields *avg_response_time, *count_200
 | foreach *avg_response_time [eval total_avg_response_time = '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;']
 | foreach *count_200 [eval total_count_200 = '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;']
 | fields total_avg_response_time, total_count_200
 | eval avg_response_time = total_avg_response_time / total_count_200
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;If there are multiple events the above should still compute the avg_response_time per event.&lt;BR /&gt;
Please let me know if that's what you are looking for.&lt;/P&gt;

&lt;P&gt;Thanks,&lt;BR /&gt;
J&lt;/P&gt;</description>
    <pubDate>Tue, 29 Sep 2020 08:42:12 GMT</pubDate>
    <dc:creator>javiergn</dc:creator>
    <dc:date>2020-09-29T08:42:12Z</dc:date>
    <item>
      <title>How to extract fields from JSON data based on API calls?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-extract-fields-from-JSON-data-based-on-API-calls/m-p/264573#M79409</link>
      <description>&lt;P&gt;Hi &lt;/P&gt;

&lt;P&gt;I have the below json file in Splunk.  How do I extract based on api calls? Eg.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;apiname                 count200   count500   avgresponse-time
HomeLoanService         150        2          0.02
CreditAccountServvice   323        19         0.04
....
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;avgresponse time is = sum(avgresponse_time)/sum(count_200)&lt;/P&gt;

&lt;P&gt;Each json event:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;{
   "date_time":"30/Jan/2016:00:00:20-0800",
   "total_events":13,
   "api_calls":{
      "HomeLoanService":{
         "count_200":1,
         "count_500":0,
         "avg_response_time":"0.0190"
      },
      "CreditAcccountService":{
         "count_200":2,
         "count_500":0,
         "avg_response_time":"0.538"
      },
      "RegisterService":{
         "count_200":1,
         "count_500":0,
         "avg_response_time":"0.0470"
      },
      "TransactionService":{
         "count_200":1,
         "count_500":0,
         "avg_response_time":"0.186"
      },
      "getNewUsers":{
         "count_200":2,
         "count_500":0,
         "avg_response_time":"0.0620"
      },
      "LockServices":{
         "count_200":1,
         "count_500":0,
         "avg_response_time":"0.0200"
      },
      "ValidateService":{
         "count_200":4,
         "count_500":1,
         "avg_response_time":"0.0210"
      }
   }
}
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Sep 2020 08:38:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-extract-fields-from-JSON-data-based-on-API-calls/m-p/264573#M79409</guid>
      <dc:creator>anasar</dc:creator>
      <dc:date>2020-09-29T08:38:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract fields from JSON data based on API calls?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-extract-fields-from-JSON-data-based-on-API-calls/m-p/264574#M79410</link>
      <description>&lt;P&gt;Taking your example as above this is what I've done (you can ignore the stats count as this is needed to generate a dummy row):&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| stats count
| eval myjson = "
{
\"date_time\":\"30/Jan/2016:00:00:20-0800\",
\"total_events\":13,
\"api_calls\":{
\"HomeLoanService\":{
\"count_200\":1,
\"count_500\":0,
\"avg_response_time\":\"0.0190\"
},
\"CreditAcccountService\":{
\"count_200\":2,
\"count_500\":0,
\"avg_response_time\":\"0.538\"
},
\"RegisterService\":{
\"count_200\":1,
\"count_500\":0,
\"avg_response_time\":\"0.0470\"
},
\"TransactionService\":{
\"count_200\":1,
\"count_500\":0,
\"avg_response_time\":\"0.186\"
},
\"getNewUsers\":{
\"count_200\":2,
\"count_500\":0,
\"avg_response_time\":\"0.0620\"
},
\"LockServices\":{
\"count_200\":1,
\"count_500\":0,
\"avg_response_time\":\"0.0200\"
},
\"ValidateService\":{
\"count_200\":4,
\"count_500\":1,
\"avg_response_time\":\"0.0210\"
}
}
}"
| spath input=myjson
| fields *avg_response_time, *count_200
| foreach *avg_response_time [eval total_avg_response_time = '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;']
| foreach *count_200 [eval total_count_200 = '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;']
| fields total_avg_response_time, total_count_200
| eval avg_response_time = total_avg_response_time / total_count_200
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;And the output:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;total_avg_response_time     total_count_200     avg_response_time
0.0620  2   0.0310 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Hope that helps&lt;/P&gt;</description>
      <pubDate>Wed, 03 Feb 2016 15:36:24 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-extract-fields-from-JSON-data-based-on-API-calls/m-p/264574#M79410</guid>
      <dc:creator>javiergn</dc:creator>
      <dc:date>2016-02-03T15:36:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract fields from JSON data based on API calls?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-extract-fields-from-JSON-data-based-on-API-calls/m-p/264575#M79411</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;&lt;BR /&gt;
This event is already indexed.  I've just shown a single record(event).  If we have multiple such events,  pls let me know how to consolidate.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Feb 2016 13:21:50 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-extract-fields-from-JSON-data-based-on-API-calls/m-p/264575#M79411</guid>
      <dc:creator>anasar</dc:creator>
      <dc:date>2016-02-09T13:21:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract fields from JSON data based on API calls?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-extract-fields-from-JSON-data-based-on-API-calls/m-p/264576#M79412</link>
      <description>&lt;P&gt;If you JSON events have already been extracted correctly then you just need the following bits:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; your search query here
 | fields *avg_response_time, *count_200
 | foreach *avg_response_time [eval total_avg_response_time = '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;']
 | foreach *count_200 [eval total_count_200 = '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;']
 | fields total_avg_response_time, total_count_200
 | eval avg_response_time = total_avg_response_time / total_count_200
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;If there are multiple events the above should still compute the avg_response_time per event.&lt;BR /&gt;
Please let me know if that's what you are looking for.&lt;/P&gt;

&lt;P&gt;Thanks,&lt;BR /&gt;
J&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 08:42:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-extract-fields-from-JSON-data-based-on-API-calls/m-p/264576#M79412</guid>
      <dc:creator>javiergn</dc:creator>
      <dc:date>2020-09-29T08:42:12Z</dc:date>
    </item>
  </channel>
</rss>

