<?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 access a property on the last element in an array,accessing last element in json array? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-access-a-property-on-the-last-element-in-an-array/m-p/469479#M132099</link>
    <description>&lt;P&gt;Can you provide sample JSON?&lt;/P&gt;</description>
    <pubDate>Fri, 30 Aug 2019 12:45:26 GMT</pubDate>
    <dc:creator>jawaharas</dc:creator>
    <dc:date>2019-08-30T12:45:26Z</dc:date>
    <item>
      <title>How to access a property on the last element in an array,accessing last element in json array?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-access-a-property-on-the-last-element-in-an-array/m-p/469478#M132098</link>
      <description>&lt;P&gt;Hi I have json events that have an array with objects and i want to extract a property from it&lt;/P&gt;

&lt;P&gt;Some pseudo search code&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| spath output=LastResult  path=message.results{-1}
| table LastResult.timestamp
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;{-1} indexing does not seem to work in spath&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| spath output=Results  path=message.results{}
| eval LastResult=mvindex(Results, -1)
| table LastResult.timestamp
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Also does not work because LastResult has become a string version of the last array element so .timestamp does not work on that string.&lt;/P&gt;

&lt;P&gt;my actual objects are a bit more complex and I want to get multiple properties so a regex on the string returned by mvindex is not really an option.&lt;/P&gt;

&lt;P&gt;Is there a good way to do this?&lt;/P&gt;

&lt;P&gt;,I got a json that with arrays in events.&lt;BR /&gt;
I'd like to access a property of the last element in such array&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| spath output=LastResult path=message.results{-1}
| table LastResult.timestamp
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;but {-1} does not seem to work for indexing the last element&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| spath output=Results path=message.results{}
| eval LastResult= mvindex(Results, -1)
| table LastResult.timestamp
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;mvindex does accept -1 and it does get the last result from the array &lt;BR /&gt;
But also does not work because LastResult becomes a string instead of an json object and thus .timestamp does not work&lt;/P&gt;

&lt;P&gt;Is there a way to do this?&lt;/P&gt;</description>
      <pubDate>Fri, 30 Aug 2019 10:23:06 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-access-a-property-on-the-last-element-in-an-array/m-p/469478#M132098</guid>
      <dc:creator>marcovdlinden</dc:creator>
      <dc:date>2019-08-30T10:23:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to access a property on the last element in an array,accessing last element in json array?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-access-a-property-on-the-last-element-in-an-array/m-p/469479#M132099</link>
      <description>&lt;P&gt;Can you provide sample JSON?&lt;/P&gt;</description>
      <pubDate>Fri, 30 Aug 2019 12:45:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-access-a-property-on-the-last-element-in-an-array/m-p/469479#M132099</guid>
      <dc:creator>jawaharas</dc:creator>
      <dc:date>2019-08-30T12:45:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to access a property on the last element in an array,accessing last element in json array?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-access-a-property-on-the-last-element-in-an-array/m-p/469480#M132100</link>
      <description>&lt;P&gt;Hello @marcovdlinden ,&lt;/P&gt;

&lt;P&gt;check this. It should solve you problem.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults 
| eval _raw = "{\"message\":{\"results\":[1,2,3], \"otherFields\":0}"
| spath output=result message.results{}
| eval res = mvindex(result,mvcount(result)-1)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 30 Aug 2019 14:33:58 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-access-a-property-on-the-last-element-in-an-array/m-p/469480#M132100</guid>
      <dc:creator>poete</dc:creator>
      <dc:date>2019-08-30T14:33:58Z</dc:date>
    </item>
  </channel>
</rss>

