# JSON date conversion

**URL:** <https://discuss.elastic.co/t/json-date-conversion/68967>\
**Category:** Logstash\
**Created:** [December 14, 2016, 5:12am UTC](https://discuss.elastic.co/t/json-date-conversion/68967 "2016-12-14T05:12:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![shambar](https://avatars.discourse-cdn.com/v4/letter/s/e9bcb4/32.png) [@shambar](https://discuss.elastic.co/u/shambar)\
**Post date:** [December 14, 2016, 5:12am UTC](https://discuss.elastic.co/t/json-date-conversion/68967/1 "2016-12-14T05:12:57Z")

</div>

Is there an easy way for logstash to convert a date form json date format to a readable format? and put it back in the same variable?

the date comes in like {"srcdate":"/Date(1164258000000)/"}

I would like to get the date into the format MM/dd/YYYY

I am using logstash 2.2.0

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [December 14, 2016, 6:40am UTC](https://discuss.elastic.co/t/json-date-conversion/68967/2 "2016-12-14T06:40:25Z")

</div>

There is no "JSON date format". What does 1164258000000 mean? It doesn't appear to be either seconds or milliseconds since the epoch.

---

<div class="post-metadata">

**Author:** ![shambar](https://avatars.discourse-cdn.com/v4/letter/s/e9bcb4/32.png) [@shambar](https://discuss.elastic.co/u/shambar)\
**Post date:** [December 14, 2016, 1:44pm UTC](https://discuss.elastic.co/t/json-date-conversion/68967/3 "2016-12-14T13:44:27Z")

</div>

I am using powershell to output the json it provides the date.

here is the powershell script to get their date information  
Code:  
$test = get-date  
$test1=$test | convertto-json

$test  
$test1

Output:  
Wednesday, December 14, 2016 8:38:33 AM  
{  
"value": "/Date(1481722713595)/",  
"DisplayHint": 2,  
"DateTime": "Wednesday, December 14, 2016 8:38:33 AM"  
}

Also I am using logstash 2.2.0

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [December 14, 2016, 1:59pm UTC](https://discuss.elastic.co/t/json-date-conversion/68967/4 "2016-12-14T13:59:47Z")

</div>

Okay, but I still don't understand what that number represents.

---

<div class="post-metadata">

**Author:** ![shambar](https://avatars.discourse-cdn.com/v4/letter/s/e9bcb4/32.png) [@shambar](https://discuss.elastic.co/u/shambar)\
**Post date:** [December 15, 2016, 3:55pm UTC](https://discuss.elastic.co/t/json-date-conversion/68967/5 "2016-12-15T15:55:43Z")

</div>

this syntax works in powershell and formats into the number of milliseconds.

$target = "2/2/2014 6:32 PM"  
[int64]((get-date $target).addhours((([datetime]::UtcNow)-(get-date)).Hours)-(get-date "1/1/1970")).totalmilliseconds

It was taken from [http://stackoverflow.com/questions/26067906/format-a-datetime-in-powershell-to-json-as-date1411704000000](http://stackoverflow.com/questions/26067906/format-a-datetime-in-powershell-to-json-as-date1411704000000)

Basically the converto-json function in powershell converts it to value": "/Date(1481722713595)/", which is valid json but not a valid date for logstash or elastic to use. I have written some code to alleviate this but it is basically loop and very expensive. I did not know if there was a way for logstash to manipulate the date and put it back into the same variable.

Data will come in like

"startdate": "/Date(1481722713595)/",  
"enddate": "/Date(1481722713595)/",

I guess I would need logstash to look at the field and determine the correct date.

the regex pattern match would be something like this '("\/\w\*\W\d\*\W\/")'

This converts the date back in powershell I have to test for negative dates as well.  
#negative test  
$mydate = {"/Date(-366235200000)/"}|convertfrom-json  
$mydate1 ="$($mydate.toshortdatestring())"  
$mydate1

# positive test

$mydate = {"/Date(1174363200001)/"}|convertfrom-json  
$mydate1 ="$($mydate.toshortdatestring())"  
$mydate1

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [December 15, 2016, 4:05pm UTC](https://discuss.elastic.co/t/json-date-conversion/68967/6 "2016-12-15T16:05:38Z")

</div>

> this syntax works in powershell and formats into the number of milliseconds.

Good. You should be able to use a date filter with a UNIX\_MS pattern to parse it.

> I guess I would need logstash to look at the field and determine the correct date.

Use a grok filter to extract the number into a field of its own, then use a date filter (with a UNIX\_MS pattern) to parse the timestamp.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 12, 2017, 4:05pm UTC](https://discuss.elastic.co/t/json-date-conversion/68967/7 "2017-01-12T16:05:48Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
