<?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 to get the value of a field inside a matching object of a multivalue field in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-the-value-of-a-field-inside-a-matching-object-of-a/m-p/679842#M232391</link>
    <description>&lt;P&gt;I have a json that looks like this:&lt;/P&gt;&lt;P&gt;{&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;"Field1" : [&lt;/P&gt;&lt;P class="lia-indent-padding-left-60px"&gt;{&lt;/P&gt;&lt;P class="lia-indent-padding-left-90px"&gt;"id": 1234&lt;/P&gt;&lt;P class="lia-indent-padding-left-90px"&gt;"name": "John"&lt;/P&gt;&lt;P class="lia-indent-padding-left-60px"&gt;},&lt;/P&gt;&lt;P class="lia-indent-padding-left-60px"&gt;{&lt;/P&gt;&lt;P class="lia-indent-padding-left-90px"&gt;"id": 5678&lt;/P&gt;&lt;P class="lia-indent-padding-left-90px"&gt;"name": "Mary"&lt;/P&gt;&lt;P class="lia-indent-padding-left-90px"&gt;"occupation": {&lt;/P&gt;&lt;P class="lia-indent-padding-left-120px"&gt;"title": "lawyer",&lt;/P&gt;&lt;P class="lia-indent-padding-left-120px"&gt;"employer": "law firm"&lt;/P&gt;&lt;P class="lia-indent-padding-left-90px"&gt;}&lt;/P&gt;&lt;P class="lia-indent-padding-left-60px"&gt;}&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;]&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;I want to extract the value of the "name" field &lt;STRONG&gt;from the object that contains an occupation field (could be any)&lt;/STRONG&gt;. In this case I want to get "Mary" and store it inside a variable. How would I do this using splunk search language?&lt;/P&gt;</description>
    <pubDate>Wed, 06 Mar 2024 16:58:54 GMT</pubDate>
    <dc:creator>junaedsa</dc:creator>
    <dc:date>2024-03-06T16:58:54Z</dc:date>
    <item>
      <title>How to get the value of a field inside a matching object of a multivalue field</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-the-value-of-a-field-inside-a-matching-object-of-a/m-p/679842#M232391</link>
      <description>&lt;P&gt;I have a json that looks like this:&lt;/P&gt;&lt;P&gt;{&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;"Field1" : [&lt;/P&gt;&lt;P class="lia-indent-padding-left-60px"&gt;{&lt;/P&gt;&lt;P class="lia-indent-padding-left-90px"&gt;"id": 1234&lt;/P&gt;&lt;P class="lia-indent-padding-left-90px"&gt;"name": "John"&lt;/P&gt;&lt;P class="lia-indent-padding-left-60px"&gt;},&lt;/P&gt;&lt;P class="lia-indent-padding-left-60px"&gt;{&lt;/P&gt;&lt;P class="lia-indent-padding-left-90px"&gt;"id": 5678&lt;/P&gt;&lt;P class="lia-indent-padding-left-90px"&gt;"name": "Mary"&lt;/P&gt;&lt;P class="lia-indent-padding-left-90px"&gt;"occupation": {&lt;/P&gt;&lt;P class="lia-indent-padding-left-120px"&gt;"title": "lawyer",&lt;/P&gt;&lt;P class="lia-indent-padding-left-120px"&gt;"employer": "law firm"&lt;/P&gt;&lt;P class="lia-indent-padding-left-90px"&gt;}&lt;/P&gt;&lt;P class="lia-indent-padding-left-60px"&gt;}&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;]&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;P&gt;I want to extract the value of the "name" field &lt;STRONG&gt;from the object that contains an occupation field (could be any)&lt;/STRONG&gt;. In this case I want to get "Mary" and store it inside a variable. How would I do this using splunk search language?&lt;/P&gt;</description>
      <pubDate>Wed, 06 Mar 2024 16:58:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-get-the-value-of-a-field-inside-a-matching-object-of-a/m-p/679842#M232391</guid>
      <dc:creator>junaedsa</dc:creator>
      <dc:date>2024-03-06T16:58:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the value of a field inside a matching object of a multivalue field</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-the-value-of-a-field-inside-a-matching-object-of-a/m-p/679846#M232392</link>
      <description>&lt;LI-CODE lang="markup"&gt;| spath Field1{} output=Field1
| mvexpand Field1
| spath input=Field1 occupation
| where isnotnull(occupation)
| spath input=Field1 name
| table name&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 06 Mar 2024 17:20:35 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-get-the-value-of-a-field-inside-a-matching-object-of-a/m-p/679846#M232392</guid>
      <dc:creator>ITWhisperer</dc:creator>
      <dc:date>2024-03-06T17:20:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the value of a field inside a matching object of a multivalue field</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-get-the-value-of-a-field-inside-a-matching-object-of-a/m-p/679848#M232394</link>
      <description>&lt;P&gt;Thank you for illustrating input in text format. &amp;nbsp;But please make sure JSON is conformant when doing mockups.&lt;/P&gt;&lt;P&gt;Speaking of JSON, I always say do not treat structured data as text. &amp;nbsp;regex is not a suitable tool for structured data in most cases. &amp;nbsp;Splunk's robust, QA tested tool will save you countless hours down the road. &amp;nbsp;Traditional tool for this is &lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Spath" target="_blank" rel="noopener"&gt;spath&lt;/A&gt;. &amp;nbsp;Since 9.0, Splunk also added &lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Fromjson" target="_blank" rel="noopener"&gt;fromjson&lt;/A&gt; that can simplify this work. &amp;nbsp;I'll begin with the simpler one. &amp;nbsp;You didn't say which field the JSON is in, so I'll assume that's _raw in the following.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| fromjson _raw
| mvexpand Field1
| fromjson Field1&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;This gives you&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Field1&lt;/TD&gt;&lt;TD&gt;id&lt;/TD&gt;&lt;TD&gt;name&lt;/TD&gt;&lt;TD&gt;occupation&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;{"id":1234,"name":"John"}&lt;/TD&gt;&lt;TD&gt;1234&lt;/TD&gt;&lt;TD&gt;John&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;{"id":5678,"name":"Mary","occupation":{"title":"lawyer","employer":"law firm"}}&lt;/TD&gt;&lt;TD&gt;5678&lt;/TD&gt;&lt;TD&gt;Mary&lt;/TD&gt;&lt;TD&gt;{"title":"lawyer","employer":"law firm"}&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;The spath alternative is - again assuming JSON is in _raw&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| spath path=Field1{}
| mvexpand Field1{}
| spath input=Field1{}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This gives&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Field1{}&lt;/TD&gt;&lt;TD&gt;id&lt;/TD&gt;&lt;TD&gt;name&lt;/TD&gt;&lt;TD&gt;occupation.employer&lt;/TD&gt;&lt;TD&gt;occupation.title&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;{ "id": 1234, "name": "John" }&lt;/TD&gt;&lt;TD&gt;1234&lt;/TD&gt;&lt;TD&gt;John&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;{ "id": 5678, "name": "Mary", "occupation": { "title": "lawyer", "employer": "law firm" } }&lt;/TD&gt;&lt;TD&gt;5678&lt;/TD&gt;&lt;TD&gt;Mary&lt;/TD&gt;&lt;TD&gt;law firm&lt;/TD&gt;&lt;TD&gt;lawyer&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;There &amp;nbsp;can be many variants in between. &amp;nbsp;But the essence is to extract elements of the JSON array, then handle the array as a multivalue field as a whole. &amp;nbsp;If, for example, there are too many elements and you worry about RAM, you can use &lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Mvfilter" target="_blank" rel="noopener"&gt;mvfilter&lt;/A&gt; to get data about Mary as you are not interested in other entries:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| fromjson _raw
| eval of_interest = mvfilter(json_extract(Field1, "name") == "Mary")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(Note you need 8.0 to use&amp;nbsp;&lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/JSONFunctions#json_extract.28.26lt.3Bjson.26gt.3B.2C_.26lt.3Bpaths.26gt.3B.29" target="_blank" rel="noopener"&gt;json_extract&lt;/A&gt;.) You get&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;DIV class=""&gt;Field1&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;of_interest&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;DIV class=""&gt;{"id":1234,"name":"John"}&lt;/DIV&gt;&lt;DIV class=""&gt;{"id":5678,"name":"Mary","occupation":{"title":"lawyer","employer":"law firm"}}&lt;/DIV&gt;&lt;/TD&gt;&lt;TD&gt;{"id":5678,"name":"Mary","occupation":{"title":"lawyer","employer":"law firm"}}&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;Hope this helps.&lt;/P&gt;&lt;P&gt;By the way, the conformant form of your mock data is&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;{

"Field1" : [

{

"id": 1234,

"name": "John"

},

{

"id": 5678,

"name": "Mary",

"occupation": {

"title": "lawyer",

"employer": "law firm"

}

}

]

}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can play with the following emulation 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 = "{

\"Field1\" : [

{

\"id\": 1234,

\"name\": \"John\"

},

{

\"id\": 5678,

\"name\": \"Mary\",

\"occupation\": {

\"title\": \"lawyer\",

\"employer\": \"law firm\"

}

}

]

}"
``` data emulation above ```&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Mar 2024 17:27:33 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-get-the-value-of-a-field-inside-a-matching-object-of-a/m-p/679848#M232394</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2024-03-06T17:27:33Z</dc:date>
    </item>
  </channel>
</rss>

