<?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 rename field with numeric date eg 2024-06-10 to today in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/rename-field-with-numeric-date-eg-2024-06-10-to-today/m-p/690208#M235093</link>
    <description>&lt;P&gt;Hi Community,&lt;/P&gt;
&lt;P&gt;actual i have a cron job, thats get every day values for today and tomorrow.&lt;BR /&gt;How to extract for "today" or "tomorrow" the value?&lt;BR /&gt;&lt;BR /&gt;This SPL doesn´t work, and don´t&amp;nbsp; rename my field to get a fix fieldname...&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;| eval today=strftime(_time,"%Y-%m-%d")
| rename "result."+'today' AS "result_today"
| stats list(result_today)&lt;/LI-CODE&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;Here my RAW...&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="CMEOGNAD_0-1718013750277.png" style="width: 400px;"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/31251i1932CE998F4EA978/image-size/medium?v=v2&amp;amp;px=400" role="button" title="CMEOGNAD_0-1718013750277.png" alt="CMEOGNAD_0-1718013750277.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 10 Jun 2024 10:37:22 GMT</pubDate>
    <dc:creator>CMEOGNAD</dc:creator>
    <dc:date>2024-06-10T10:37:22Z</dc:date>
    <item>
      <title>rename field with numeric date eg 2024-06-10 to today</title>
      <link>https://community.splunk.com/t5/Splunk-Search/rename-field-with-numeric-date-eg-2024-06-10-to-today/m-p/690208#M235093</link>
      <description>&lt;P&gt;Hi Community,&lt;/P&gt;
&lt;P&gt;actual i have a cron job, thats get every day values for today and tomorrow.&lt;BR /&gt;How to extract for "today" or "tomorrow" the value?&lt;BR /&gt;&lt;BR /&gt;This SPL doesn´t work, and don´t&amp;nbsp; rename my field to get a fix fieldname...&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;| eval today=strftime(_time,"%Y-%m-%d")
| rename "result."+'today' AS "result_today"
| stats list(result_today)&lt;/LI-CODE&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;Here my RAW...&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="CMEOGNAD_0-1718013750277.png" style="width: 400px;"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/31251i1932CE998F4EA978/image-size/medium?v=v2&amp;amp;px=400" role="button" title="CMEOGNAD_0-1718013750277.png" alt="CMEOGNAD_0-1718013750277.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Jun 2024 10:37:22 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/rename-field-with-numeric-date-eg-2024-06-10-to-today/m-p/690208#M235093</guid>
      <dc:creator>CMEOGNAD</dc:creator>
      <dc:date>2024-06-10T10:37:22Z</dc:date>
    </item>
    <item>
      <title>Re: rename field with numeric date eg 2024-06-10 to today</title>
      <link>https://community.splunk.com/t5/Splunk-Search/rename-field-with-numeric-date-eg-2024-06-10-to-today/m-p/690295#M235121</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/243177"&gt;@CMEOGNAD&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Assuming that today's date will always be the first element of result and tomorrow the second, you can do this:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;```adding sample data```
| makeresults 
| eval _raw="{
     \"result\" : [
         {\"2024-06-10\" : 1338},
         {\"2024-06-11\" : 1715}
      ]
}"

```using spath to extract values```
| spath output=today path=result{0}.
| rex field=today "\{\"(?&amp;lt;todayDate&amp;gt;[^\"]+)\"\s\:\s(?&amp;lt;todayResult&amp;gt;\d+)"
| spath output=tomorrow path=result{1}.
| rex field=today "\{\"(?&amp;lt;tomorrowDate&amp;gt;[^\"]+)\"\s\:\s(?&amp;lt;tomorrowResult&amp;gt;\d+)"&lt;/LI-CODE&gt;&lt;P&gt;This gets you the todayResult and tomorrowResult values extracted with regex.&lt;BR /&gt;&lt;BR /&gt;Ideally, you could extract the values directly with spath, but it seems it's &lt;STRONG&gt;not&lt;/STRONG&gt; possible to use a variable for the path in spath, e.g.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| eval today=tostring(strftime(_time,"%Y-%m-%d"))
| spath output=today path=result{0}.
| spath input=today output=today path='today'&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;hope this helps.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jun 2024 04:05:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/rename-field-with-numeric-date-eg-2024-06-10-to-today/m-p/690295#M235121</guid>
      <dc:creator>KendallW</dc:creator>
      <dc:date>2024-06-11T04:05:05Z</dc:date>
    </item>
    <item>
      <title>Re: rename field with numeric date eg 2024-06-10 to today</title>
      <link>https://community.splunk.com/t5/Splunk-Search/rename-field-with-numeric-date-eg-2024-06-10-to-today/m-p/690301#M235125</link>
      <description>&lt;P&gt;Iterator command&amp;nbsp;&lt;A href="https://docs.splunk.com/Documentation/Splunk/9.2.1/SearchReference/Foreach" target="_blank" rel="noopener"&gt;foreach&lt;/A&gt;&amp;nbsp;is your friend. (I do not recommend treating structured data as text.) &amp;nbsp;One strategy could be iterate on field name:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| foreach result.*
    [eval today = mvappend(today, if(strftime(now(), "%F") == "&amp;lt;&amp;lt;MATCHSTR&amp;gt;&amp;gt;", '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;', null())),
    tomorrow = mvappend(tomorrow, if(strftime(now(), "%F") != "&amp;lt;&amp;lt;MATCHSTR&amp;gt;&amp;gt;", '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;', null()))]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;BTW, illustrate raw JSON in text, do not use screenshot and do not use Splunk's contracted format. &amp;nbsp;Using&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/121137"&gt;@KendallW&lt;/a&gt;'s makeresults emulation, you get&lt;/P&gt;&lt;TABLE width="617px"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="172.84375px"&gt;_time&lt;/TD&gt;&lt;TD width="149.734375px"&gt;result.2024-06-10&lt;/TD&gt;&lt;TD width="153.75px"&gt;result.2024-06-11&lt;/TD&gt;&lt;TD width="52.265625px"&gt;today&lt;/TD&gt;&lt;TD width="87.4375px"&gt;tomorrow&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="172.84375px"&gt;2024-06-10 23:59:39&lt;/TD&gt;&lt;TD width="149.734375px"&gt;1338&lt;/TD&gt;&lt;TD width="153.75px"&gt;1715&lt;/TD&gt;&lt;TD width="52.265625px"&gt;1338&lt;/TD&gt;&lt;TD width="87.4375px"&gt;1715&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Tue, 11 Jun 2024 07:04:29 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/rename-field-with-numeric-date-eg-2024-06-10-to-today/m-p/690301#M235125</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2024-06-11T07:04:29Z</dc:date>
    </item>
    <item>
      <title>Re: rename field with numeric date eg 2024-06-10 to today</title>
      <link>https://community.splunk.com/t5/Splunk-Search/rename-field-with-numeric-date-eg-2024-06-10-to-today/m-p/690305#M235127</link>
      <description>&lt;P&gt;You won't really be able to rename the fields unless you transpose data, which is probably not the right approach in your use case.&lt;/P&gt;&lt;P&gt;Here are a couple of other examples to give you ways to manipulate data&lt;/P&gt;&lt;P&gt;This one gets the result object and sorts the dates to make sure they are in date order&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| eval dates=mvsort(json_array_to_mv(json_extract(_raw, "result")))
| eval result_today=replace(mvindex(dates, 0), "[^:]*:\s*(\d+)\}", "\1")
| eval result_tomorrow=replace(mvindex(dates, 1), "[^:]*:\s*(\d+)\}", "\1")&lt;/LI-CODE&gt;&lt;P&gt;This one extracts the fields and then uses the wildcarding technique with foreach to make the field assignments.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| spath
| foreach result{}.* [ eval result_today=if("&amp;lt;&amp;lt;MATCHSTR&amp;gt;&amp;gt;"=strftime(_time, "%F"), '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;', result_today),
                            result_tomorrow=if("&amp;lt;&amp;lt;MATCHSTR&amp;gt;&amp;gt;"=strftime(_time, "%F"), result_tomorrow, '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;')]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Jun 2024 07:09:25 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/rename-field-with-numeric-date-eg-2024-06-10-to-today/m-p/690305#M235127</guid>
      <dc:creator>bowesmana</dc:creator>
      <dc:date>2024-06-11T07:09:25Z</dc:date>
    </item>
    <item>
      <title>Re: rename field with numeric date eg 2024-06-10 to today</title>
      <link>https://community.splunk.com/t5/Splunk-Search/rename-field-with-numeric-date-eg-2024-06-10-to-today/m-p/693972#M236064</link>
      <description>&lt;P&gt;The solution from yuanliu works, but not for the full json file from &lt;A href="https://forecast.solar/" target="_blank"&gt;https://forecast.solar/&lt;/A&gt;&lt;BR /&gt;The best way was to use regex field extractor, but...&lt;BR /&gt;&lt;BR /&gt;...next step to get timecharts from this format wont work by regex&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;{
    "result": {
        "watts": {
            "2019-06-22 05:15:00": 17,
            "2019-06-22 05:30:00": 22,
            "2019-06-22 05:45:00": 27,
            ...
            "2019-06-29 20:15:00": 14,
            "2019-06-29 20:30:00": 11,
            "2019-06-29 20:45:00": 7
        },
        "watt_hours": {
            "2019-06-22 05:15:00": 0,
            "2019-06-22 05:30:00": 6,
            "2019-06-22 05:45:00": 12,
            ...
            "2019-06-29 20:15:00": 2545,
            "2019-06-29 20:30:00": 2548,
            "2019-06-29 20:45:00": 2550
        },
        "watt_hours_day": {
            "2019-06-22": 2626,
            "2019-06-23": 2918,
            "2019-06-24": 2526,
            "2019-06-25": 2866,
            "2019-06-26": 2892,
            "2019-06-27": 1900,
            "2019-06-28": 2199,
            "2019-06-29": 2550
        }
    },
    "message": {
        "type": "success",
        "code": 0,
        "text": ""
    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jul 2024 13:51:17 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/rename-field-with-numeric-date-eg-2024-06-10-to-today/m-p/693972#M236064</guid>
      <dc:creator>CMEOGNAD</dc:creator>
      <dc:date>2024-07-22T13:51:17Z</dc:date>
    </item>
    <item>
      <title>Re: rename field with numeric date eg 2024-06-10 to today</title>
      <link>https://community.splunk.com/t5/Splunk-Search/rename-field-with-numeric-date-eg-2024-06-10-to-today/m-p/693985#M236069</link>
      <description>&lt;P&gt;As we always say in this forum, illustration of raw input (in text format) is critical for the question to be answerable.&amp;nbsp; Thank you for finally getting to data.&amp;nbsp; My previous answer was based on KendallW's emulation.&amp;nbsp; This latest illustration is not only different from that emulation, but also different from your initial screenshot.&amp;nbsp; One fundamental difference is that this data includes multiple days potentially in the future.&amp;nbsp; It seems that the input is from a prediction of sorts.&lt;/P&gt;&lt;P&gt;This said, I also realized that JSON keys themselves can be utilized to simply solution if you are using Splunk 8.1 or later.&amp;nbsp; Again, regex is NOT the correct tool for structured data.&lt;/P&gt;&lt;P&gt;Here is the code you can try:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| eval today = strftime(now(), "%F"), tomorrow = strftime(relative_time(now(), "+1d"), "%F")
| eval today = json_extract(_raw, "result.watt_hours_day." . today)
| eval tomorrow = json_extract(_raw, "result.watt_hours_day." . tomorrow)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is an emulation for you to play with and compare with real data.&amp;nbsp; Because your illustrated data is way in the past, I randomly pick 2019-06-26 as search time and establishes a "fake_now" field instead of using now() function. (As a result, "tomorrow" corresponds to 2019-06-27.)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults
| eval _raw="{
    \"result\": {
        \"watts\": {
            \"2019-06-22 05:15:00\": 17,
            \"2019-06-22 05:30:00\": 22,
            \"2019-06-22 05:45:00\": 27,
            \"2019-06-29 20:15:00\": 14,
            \"2019-06-29 20:30:00\": 11,
            \"2019-06-29 20:45:00\": 7
        },
        \"watt_hours\": {
            \"2019-06-22 05:15:00\": 0,
            \"2019-06-22 05:30:00\": 6,
            \"2019-06-22 05:45:00\": 12,
            \"2019-06-29 20:15:00\": 2545,
            \"2019-06-29 20:30:00\": 2548,
            \"2019-06-29 20:45:00\": 2550
        },
        \"watt_hours_day\": {
            \"2019-06-22\": 2626,
            \"2019-06-23\": 2918,
            \"2019-06-24\": 2526,
            \"2019-06-25\": 2866,
            \"2019-06-26\": 2892,
            \"2019-06-27\": 1900,
            \"2019-06-28\": 2199,
            \"2019-06-29\": 2550
        }
    },
    \"message\": {
        \"type\": \"success\",
        \"code\": 0,
        \"text\": \"\"
    }
}"
| spath
| eval fake_now = strptime("2019-06-26 18:15:06", "%F %T")
| eval today = strftime(fake_now, "%F"), tomorrow = strftime(relative_time(fake_now, "+1d"), "%F")
| eval today = json_extract(_raw, "result.watt_hours_day." . today)
| eval tomorrow = json_extract(_raw, "result.watt_hours_day." . tomorrow)
| fields result.watt_hours_day.2019-06-26 result.watt_hours_day.2019-06-27 today tomorrow&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Output is&lt;/P&gt;&lt;TABLE width="740px"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="52px"&gt;today&lt;/TD&gt;&lt;TD width="82.203125px"&gt;tomorrow&lt;/TD&gt;&lt;TD width="219px"&gt;result.watt_hours_day.2019-06-26&lt;/TD&gt;&lt;TD width="219px"&gt;result.watt_hours_day.2019-06-27&lt;/TD&gt;&lt;TD width="166.828125px"&gt;_raw&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="52px"&gt;2892&lt;/TD&gt;&lt;TD width="82.203125px"&gt;1900&lt;/TD&gt;&lt;TD width="219px"&gt;2892&lt;/TD&gt;&lt;TD width="219px"&gt;1900&lt;/TD&gt;&lt;TD width="166.828125px"&gt;{ "result": { "watts": { "2019-06-22 05:15:00": 17, "2019-06-22 05:30:00": 22, "2019-06-22 05:45:00": 27, "2019-06-29 20:15:00": 14, "2019-06-29 20:30:00": 11, "2019-06-29 20:45:00": 7 }, "watt_hours": { "2019-06-22 05:15:00": 0, "2019-06-22 05:30:00": 6, "2019-06-22 05:45:00": 12, "2019-06-29 20:15:00": 2545, "2019-06-29 20:30:00": 2548, "2019-06-29 20:45:00": 2550 }, "watt_hours_day": { "2019-06-22": 2626, "2019-06-23": 2918, "2019-06-24": 2526, "2019-06-25": 2866, "2019-06-26": 2892, "2019-06-27": 1900, "2019-06-28": 2199, "2019-06-29": 2550 } }, "message": { "type": "success", "code": 0, "text": "" } }&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Mon, 22 Jul 2024 17:15:17 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/rename-field-with-numeric-date-eg-2024-06-10-to-today/m-p/693985#M236069</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2024-07-22T17:15:17Z</dc:date>
    </item>
  </channel>
</rss>

