jsonslice icon indicating copy to clipboard operation
jsonslice copied to clipboard

I want to extend the conditional filter date type as shown in the above example. What interface or method do I need to implement?

Open GuoxinL opened this issue 5 years ago • 5 comments

json := `{
  "arr": [
    {
      "name": "jack",
      "create_time": "2020-01-01",
      "age": 10
    },
    {
      "name": "wow",
      "create_time": "2020-02-01",
      "age": 11
    }
  ]
}`
jsonslice.Get([]byte(json), "$.arr[?(@.create_time > format("yyyy-mm-dd", 2019-12-30))].name")

I want to extend the conditional filter date type as shown in the above example. What interface or method do I need to implement?

GuoxinL avatar Mar 17 '20 15:03 GuoxinL

I believe there are two questions implied here: what is the best way to specify a date (or date/time) constant and how to convert json date to comparable value (e.g. to give jsonslice a hint on how to parse a "create_time" field).

The first problem can be solved either the way you mentioned (using some kind of conversion function) or by introducing a strict format for datetime constants, like "YYYY-MM-DD HH:NN:SS TZ". The second problem obviously require a conversion function since json date format may vary. Anyway, this problem is worth taking some time to think it over.

bhmj avatar Mar 20 '20 01:03 bhmj

You're right, it's really worth thinking about, for data the user knows the format of the data for example for the "create time" field (yyyy-mm-dd) and since we've been thinking about the "create time" field for date types, we can directly describe the "create time" field, If the field type is known, the type of conditional filter parameter is also known, do you feel this is ok, the specific syntax above the function is a better way of grammar expression, a better way of grammar expression, not yet thought of.

GuoxinL avatar Mar 25 '20 03:03 GuoxinL

My English is not very good, please forgive me if I don't express myself well

GuoxinL avatar Mar 25 '20 03:03 GuoxinL

I would like to try to implement this first, please ask which interface I should implement

GuoxinL avatar Apr 16 '20 08:04 GuoxinL

In my opinion the optimal way would be a function call with two parameters:
date_parse(date_string, format_string). Format string should use at least these params:

Char Meaning Examples
Y Year 02018(YYYYY), 2018(YYYY), 18(YY)
M Month in year July(MMMM), Jul(MMM), 07(MM), 7(M)
D Day of the month 08(DD), 8(D)
H/h Hour 22(HH), 10(hh), 07(HH), 7(H), 7(h)
N/n miNute 03(NN/nn), 3(N/n)
S/s Second 06(SS/ss), 6(S/s)
F/f Millisecond 006(FFF/fff), 06(FF/ff), 6(F/f)
A/a AP/PM marker A.M., a.m. AM, am
X Timezone offset in hours +03; +0300; +03:00; GMT+3; GMT+0300; GMT+03:00
Any other character as is

The hour, minute and second char can be either lowercase or uppercase. The difference is only in hour char, H for 24h notation, h for 12h notation. Minutes and seconds can be described as 'N' or 'n' just to unify the whole time notation (HH:NN:SS or hh:nn:ss).

Format string Example Note
YYYY-MM-DD HH:NN:SS 2012-12-31 23:30:00 Typical UTC time, 24h notation
YYYY-MM-DD h:n a 2012-12-31 11:30 PM The same as above, 12h AM/PM notation
YYYY-MM-DD h:n a X 2012-12-31 11:30 PM GMT+03:00 The same as above, 12h notation, Europe/Moscow timezone
DD.MM.YYYY HH-NN-SS.FFF 31.12.2012 23-30-00.032 Different dividers, 24h, milliseconds
YYYY-M-DTHH:NN 2018-12-31T23:30 Non-space character separating date and time
YYYY-MM-DDTHH:NNZX 2018-12-31 23:30Z+0300 Timezone

So in jsonpath it must look like this: jsonslice.Get([]byte(json), "$.arr[?(date_parse(@.create_time,'YYYY-MM-DD') > date_parse("2019-12-30", "YYYY-MM-DD"))].name")

bhmj avatar Apr 16 '20 09:04 bhmj