<?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 do you assign values to a variable based on a name in a nested JSON? in Getting Data In</title>
    <link>https://community.splunk.com/t5/Getting-Data-In/How-do-you-assign-values-to-a-variable-based-on-a-name-in-a/m-p/442494#M77099</link>
    <description>&lt;P&gt;Hello @sboogaar,&lt;/P&gt;

&lt;P&gt;This search should give you the value for "network.in.errors":&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;your base search
| spath path=datapoints{} | mvexpand datapoints{} | spath input=datapoints{}
| search name="network.in.errors" | table name,value
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 20 Dec 2018 12:38:19 GMT</pubDate>
    <dc:creator>whrg</dc:creator>
    <dc:date>2018-12-20T12:38:19Z</dc:date>
    <item>
      <title>How do you assign values to a variable based on a name in a nested JSON?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-do-you-assign-values-to-a-variable-based-on-a-name-in-a/m-p/442493#M77098</link>
      <description>&lt;P&gt;We are working with the following JSON:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;  {
    "datapoints": [
      {
        "name": "filesystem.inode.total1",
        "value": 15728640,
        "unit": "count",
        "timestamp": "2018-12-20T11:48:51.00524107Z",
        "tags": {
          "path": "/var/lib/docker"
        }
      },
      {
        "name": "filesystem.inode.total2",
        "value": 262144,
        "unit": "count",
        "timestamp": "2018-12-20T11:48:51.00524107Z",
        "tags": {
          "path": "/var/log/audit"
        }
      },
      {
        "name": "system.uptime",
        "value": 0,
        "unit": "count",
        "timestamp": "2018-12-20T11:48:51.006448776Z",
        "tags": {
          "interface": "docker0"
        }
      }
    ]
  }
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;What I want to do is to assign the values to a variable based on the name.&lt;/P&gt;

&lt;P&gt;e.g. &lt;/P&gt;

&lt;P&gt;eval  test = [the value field where the sibbling is network.in.errors]&lt;/P&gt;

&lt;P&gt;I don't need it to be automatically extracted. I just want to find a way on how to access the specific values based on the siblings name.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Dec 2018 12:21:39 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-do-you-assign-values-to-a-variable-based-on-a-name-in-a/m-p/442493#M77098</guid>
      <dc:creator>sboogaar</dc:creator>
      <dc:date>2018-12-20T12:21:39Z</dc:date>
    </item>
    <item>
      <title>Re: How do you assign values to a variable based on a name in a nested JSON?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-do-you-assign-values-to-a-variable-based-on-a-name-in-a/m-p/442494#M77099</link>
      <description>&lt;P&gt;Hello @sboogaar,&lt;/P&gt;

&lt;P&gt;This search should give you the value for "network.in.errors":&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;your base search
| spath path=datapoints{} | mvexpand datapoints{} | spath input=datapoints{}
| search name="network.in.errors" | table name,value
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 20 Dec 2018 12:38:19 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-do-you-assign-values-to-a-variable-based-on-a-name-in-a/m-p/442494#M77099</guid>
      <dc:creator>whrg</dc:creator>
      <dc:date>2018-12-20T12:38:19Z</dc:date>
    </item>
    <item>
      <title>Re: How do you assign values to a variable based on a name in a nested JSON?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-do-you-assign-values-to-a-variable-based-on-a-name-in-a/m-p/442495#M77100</link>
      <description>&lt;P&gt;@sboogaar &lt;/P&gt;

&lt;P&gt;Can you please try this?&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;YOUR_SEARCH
| spath datapoints{} output=datapoint 
| mvexpand datapoint 
| eval _raw=datapoint 
| kv | search name="network.in.errors"
| table name "tags.interface" "tags.path" timestamp unit value
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG&gt;My Sample Search:&lt;/STRONG&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults 
| eval _raw="{   \"datapoints\": [   {     \"name\": \"filesystem.inode.total1\",     \"value\": 15728640,     \"unit\": \"count\",     \"timestamp\": \"2018-12-20T11:48:51.00524107Z\",     \"tags\": {       \"path\": \"/var/lib/docker\"     }   },   {     \"name\": \"filesystem.inode.total2\",     \"value\": 262144,     \"unit\": \"count\",     \"timestamp\": \"2018-12-20T11:48:51.00524107Z\",     \"tags\": {       \"path\": \"/var/log/audit\"     }   },   {     \"name\": \"network.in.errors\",     \"value\": 0,     \"unit\": \"count\",     \"timestamp\": \"2018-12-20T11:48:51.006448776Z\",     \"tags\": {       \"interface\": \"docker0\"     }   } ]}" 
| spath datapoints{} output=datapoint 
| mvexpand datapoint 
| eval _raw=datapoint 
| kv | search name="network.in.errors"
| table name "tags.interface" "tags.path" timestamp unit value
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Thu, 20 Dec 2018 12:40:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-do-you-assign-values-to-a-variable-based-on-a-name-in-a/m-p/442495#M77100</guid>
      <dc:creator>kamlesh_vaghela</dc:creator>
      <dc:date>2018-12-20T12:40:41Z</dc:date>
    </item>
    <item>
      <title>Re: How do you assign values to a variable based on a name in a nested JSON?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-do-you-assign-values-to-a-variable-based-on-a-name-in-a/m-p/442496#M77101</link>
      <description>&lt;P&gt;I fixed it:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| spath 
| rename datapoints{}.name as name, datapoints{}.value as value 
| eval x=mvzip(name,value) 
| mvexpand x 
| eval x=split(x,",") 
| eval name=mvindex(x,0) 
| eval value=mvindex(x,1) 
| search name=system.uptime
| table _time, name, value, host
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 20 Dec 2018 12:48:02 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-do-you-assign-values-to-a-variable-based-on-a-name-in-a/m-p/442496#M77101</guid>
      <dc:creator>sboogaar</dc:creator>
      <dc:date>2018-12-20T12:48:02Z</dc:date>
    </item>
    <item>
      <title>Re: How do you assign values to a variable based on a name in a nested JSON?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-do-you-assign-values-to-a-variable-based-on-a-name-in-a/m-p/442497#M77102</link>
      <description>&lt;P&gt;Great @sboogaar &lt;/P&gt;

&lt;P&gt;Accept your answer to close this question.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Dec 2018 12:51:58 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-do-you-assign-values-to-a-variable-based-on-a-name-in-a/m-p/442497#M77102</guid>
      <dc:creator>kamlesh_vaghela</dc:creator>
      <dc:date>2018-12-20T12:51:58Z</dc:date>
    </item>
    <item>
      <title>Re: How do you assign values to a variable based on a name in a nested JSON?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-do-you-assign-values-to-a-variable-based-on-a-name-in-a/m-p/442498#M77103</link>
      <description>&lt;P&gt;This solution seems to work better but I do not understand why.&lt;BR /&gt;
with my own query when I try to view it by _time I cant see all the results I expect but with yours it does.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=dcos sourcetype=dcos:node:metrics 
| spath datapoints{} output=datapoint 
 | mvexpand datapoint 
 | eval _raw=datapoint 
 | kv | search name="system.uptime"
 | table name  value _time
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;vs&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=dcos sourcetype=dcos:node:metrics 
| rename datapoints{}.name as name, datapoints{}.value as value 
| eval x=mvzip(name,value) 
| mvexpand x 
| eval x=split(x,",") 
| eval name=mvindex(x,0) 
| eval value=mvindex(x,1) 
| table _time, name, value, host 
| sort - _time 
| search name=system.uptime
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;gives different results&lt;/P&gt;</description>
      <pubDate>Thu, 20 Dec 2018 14:19:45 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-do-you-assign-values-to-a-variable-based-on-a-name-in-a/m-p/442498#M77103</guid>
      <dc:creator>sboogaar</dc:creator>
      <dc:date>2018-12-20T14:19:45Z</dc:date>
    </item>
    <item>
      <title>Re: How do you assign values to a variable based on a name in a nested JSON?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-do-you-assign-values-to-a-variable-based-on-a-name-in-a/m-p/442499#M77104</link>
      <description>&lt;P&gt;Both searches returning the same result for my sample data.  But the variation in events might impact the results. So below are my some inputs for queries.&lt;/P&gt;

&lt;P&gt;Maybe my approach is different. Here I have converted all the data points into &lt;CODE&gt;_raw&lt;/CODE&gt; So I can easily extract the name and other sibling fields easily without any data loss. &lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;index=dcos sourcetype=dcos:node:metrics | spath datapoints{} output=datapoint&lt;/CODE&gt; will give me each multivalued datapoint JSON.&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;| mvexpand datapoint | eval _raw=datapoint&lt;/CODE&gt; will split multivalued datapoint to the single raw event. &lt;/P&gt;

&lt;P&gt;and finally, &lt;CODE&gt;kv&lt;/CODE&gt; command will extract JSON fields from each datapoint raw events. Even you can easily apply filters also.&lt;/P&gt;

&lt;P&gt;Your search is also working fine but I have a confusion. In your search, you have used &lt;CODE&gt;mvzip&lt;/CODE&gt; for combining the multivalued fields to keep sibling values together (see note 1). And then &lt;CODE&gt;mvexpand&lt;/CODE&gt;, which will expand zipped value, which is used to extract name and value. This &lt;CODE&gt;name&lt;/CODE&gt; field is used for searching "system.update" value.&lt;/P&gt;

&lt;P&gt;Note 1: If any data have no value or no field then for that event &lt;CODE&gt;mvzip&lt;/CODE&gt; will not return any value or will return an unexpected value. This might be a reason for missing data.&lt;/P&gt;

&lt;P&gt;If my search is working properly and it is perfect as per your requirement then I suggest you use this.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Dec 2018 17:34:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-do-you-assign-values-to-a-variable-based-on-a-name-in-a/m-p/442499#M77104</guid>
      <dc:creator>kamlesh_vaghela</dc:creator>
      <dc:date>2018-12-20T17:34:52Z</dc:date>
    </item>
    <item>
      <title>Re: How do you assign values to a variable based on a name in a nested JSON?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-do-you-assign-values-to-a-variable-based-on-a-name-in-a/m-p/442500#M77105</link>
      <description>&lt;P&gt;You may find that this will not scale well for large datasets because of the INEXCUSABLY poor performance of the &lt;CODE&gt;mvexpand&lt;/CODE&gt; command.  If you examine your &lt;CODE&gt;search log&lt;/CODE&gt; in your &lt;CODE&gt;Job inspector&lt;/CODE&gt; you will see that it tells you that your search is autofinalizing because it ran out of RAM and returning partial results.  It is also pretty inexcuseable that Splunk does not make this more clear to you without you having to dig for it.  In any case, the solution is to avoid &lt;CODE&gt;mvexpand&lt;/CODE&gt;:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;|makeresults | eval time = "2018-12-20T15:02:52 2018-12-20T15:01:59 2018-12-20T15:01:00 2018-12-20T14:59:59 2018-12-20T14:58:52 2018-12-20T14:57:52 2018-12-20T14:56:51 2018-12-20T14:55:59 2018-12-20T14:54:49 2018-12-20T14:53:52 2018-12-20T14:52:53 2018-12-20T14:52:00"
| makemv time
| mvexpand time
| rename time AS _time
| eval _time = strptime(_time, "%Y-%m-%dT%H:%M:%S")
| eval host = "ics031120105 ics031120106 ics031120107"
| makemv host
| mvexpand host
| eval name = "a b c d e f g h i j system.uptime"
| eval value = "1 2 3 4 5 6 7 8 9 10 11"
| makemv name
| makemv value

| rename COMMENT AS "Everything above generates sample event data; everything below is your solution"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Like this (for the specific approach):&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| nomv name
| eval tester = mvindex(split(name, "system.uptime"), 0)
| eval position = (len(tester) - len(replace(tester, "\s", "")))
| eval uptime = mvindex(value, position)
| table _time, uptime, host 
| sort - 0 _time
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Or like this for the general approach:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval _raw = mvzip(name,value,"=")
| fields - name value
| kv
| table _time, uptime, host 
| sort - 0 _time
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 11 Feb 2019 04:45:44 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-do-you-assign-values-to-a-variable-based-on-a-name-in-a/m-p/442500#M77105</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2019-02-11T04:45:44Z</dc:date>
    </item>
  </channel>
</rss>

