JSON Path according to specific date - json

I have the following array of JSON objects
There ist one for each week.
[
{
"valutakode": "EUR",
"valutabeskrivelse": "EURO",
"valutakurs": "10,390",
"omregningsenhet": 1,
"fomdato": "2022-10-31",
"tomdato": "2022-11-06"
},
{
"valutakode": "EUR",
"valutabeskrivelse": "EURO",
"valutakurs": "10,180",
"omregningsenhet": 1,
"fomdato": "2022-11-07",
"tomdato": "2022-11-13"
}
]
I need the object that corresponds with the current week.
Is there an option to achive this using jsonpath?
Thanks

you can try this jsonpath
$[?(#.fomdato== '2022-11-07')]
or
$[?(#.fomdato == '2022-11-07' && #.tomdato == '2022-11-13')]
You will have to figure what is the first or the last date of weeek of current date and convert it to string yyyy-mm-dd
I don't know what language are you using, but IMHO maybe it would be much easier to parse your json and use Date format to find the right object.

Related

Laravel Search in Json array of objects column where date is bigger than today

I have Json column that stores data like this :
[
{"date": "2021-02-24"},
{"date": "2021-02-20"},
{"date": "2021-04-22"},
]
and I would like to get the data which is only after today, closest thing i found is this :
$dates = Foo::whereJsonContains('dates', ["date => '2021-04-22']);
but this will search by exact date , how to search by greater than just like what we do with normal where like this :
$dates = Foo::where('date', '>', Carbon::now());
Yes, it is possible to do it directly from the database, but it won't be easy.
For example, from your data you can extract json data to get an array with
SELECT JSON_EXTRACT(your_field, '$[*].date') AS dates
Here you will get dates as an array of string and you have to cast each of the string to date and compare them with tomorrow's date.
Once you are able to do that, you can pass the sql command in DB::raw()

Is it possible to get a sorted list of attribute values from a JSON array using JSONPath

Given JSON like:
[
{
"Serial no": 994,
},
{
"Serial no": 456,
}
]
I know this query will give me an array of all Serial no values, in the order they are in the JSON: $..['Serial no']
I'm not sure exactly what sorting capabilities JSONPath has but I think you can use / and \ to sort - but how are they used to modify my query string in this case? I am only interested doing this in pure JSONPath, not JS or post-query sorting - that's easy, I just want to know if I can avoid it.
This is a source I found suggesting sorting is supported but it might be product-specific?
I'm using http://www.jsonquerytool.com/ to test this

Query ISODate on MongoDB with Google Sheets "dd-MM-yyyy HH:ss" as an output

I use a tool called Redash to query (in JSON) on MongoDB. In my collections dates are formulated in ISO, so when my query is imported (with google sheets' importdata function) to a sheet, I have to convert it to the appropriate format with a formula designed in the sheet.
I would love to integrate this operation directly in my query, that the ISO date format is directly sent to Sheets in the appropriate "dd-MM-yyyy HH:ss" format.
Any ideas ?
Many many thanks
You may be able to use the $dateToString aggregation operator inside a $project aggregation stage.
For example:
> db.test.find()
{ "_id": 0, "date": ISODate("2018-03-07T05:14:13.063Z"), "a": 1, "b": 2 }
> db.test.aggregate([
{$project: {
date: {$dateToString: {
format: '%d-%m-%Y %H:%M:%S',
date: '$date'
}},
a: '$a',
b: '$b'
}}
])
{ "_id": 0, "date": "07-03-2018 05:14:13", "a": 1, "b": 2 }
Note that although the $dateToString operator was available since MongoDB 3.0, MongoDB 3.6 adds the capability to output the string according to a specific timezone.

Opensearchsever -Search range beteen dates- JSON Restful API

I'm trying to use OpenSearchServer in one of my applications using RestFul JSON API .Can you please provide an example for querying search between 2 dates using the restful JSON api?
Below is my code so far
{"query":"test help","rows":100,
"returnedFields":[
"fileName",
"url"
]
}
Sorry for the bandwidth wastage.
To search between two dates using JSON API, we can use the "Relative date filter " .
Here's what the documentation says :
The Relative date filter can be used for this. Let's say that documents are indexed with the current date in the field indexedDate. In our example the date is expressed using the yyyyMMddHHmmss format - for instance 20141225130512 stands for the 25th of December, 2014, at 1:05:12 PM.
eg:
"filters":[
{
"negative":false,
"type":"RelativeDateFilter",
"from":{
"unit":"days",
"interval":2
},
"to":{
"unit":"days",
"interval":0
},
"field":"indexedDate",
"dateFormat":"yyyyMMddHHmmss"
}
],
Further details can be found here :http://www.opensearchserver.com/documentation/faq/querying/how_to_use_filters_on_query.md

How to format datatime for json

I''m using EF to query the database using anonymous type.
here the code I use for EF
public JsonResult OverdueEventsCustom()
{
var eventCustomOverdue = _eventCustomRepository.FindOverdueEventsCustom();
return Json(eventCustomOverdue, JsonRequestBehavior.AllowGet);
}
public IQueryable<dynamic> FindOverdueEventsCustom()
{
DateTime dateTimeNow = DateTime.UtcNow;
DateTime dateTomorrow = dateTimeNow.Date.AddDays(1);
return db.EventCustoms.Where(x => x.DateTimeStart < dateTomorrow)
.Select(y => new { y.EventId, y.EventTitle, y.DateTimeStart});
}
Inspecting using the debugger I see the properties is in this format
Date = {16/08/2012 00:00:00}
The resultfor the JSON is
[{
"EventId": 1,
"EventTitle": "Homework Math",
"DateTimeStart": "\/Date(1345108269310)\/"
}, {
"EventId": 4,
"EventTitle": "Homework help with Annie",
"DateTimeStart": "\/Date(1345108269310)\/"
}, {
"EventId": 6,
"EventTitle": "Physic laboratory",
"DateTimeStart": "\/Date(1345108269310)\/"
}]
I need the the json in this format
"DateTimeStart": "(16/08/2012)"
Any idea what i'm doing wrong here? thanks for your help
Related articles
http://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx
How do I format a Microsoft JSON date?
"\/Date(1345108269310)\/" is the correct way to pass a Date to javascript. The way I see it, you have two options here:
If you do not explicitly need the value as a date, you could just pass a string to the JSON variable, containing the pretty-printed date.
Something along the lines of:
DateTimeStart: String.Format("{0: dd-MM-yyyy}", myDate)
If you will still need to use the variable a a date in javascript (for calculations for example), the most consice and readably way would be to create a javascript function that converts said date into the pretty-printed string you want (I don't know if such a function already exists. It isn't too hard to create though:
function prettyDate(date) {
return date.getDate() + "-" + date.getMonth() + "-" + date.getFullYear();
}
I would suggest passing it along as a string from you code behind, as it is more readable. But that only works if you do not need to use the date except for displaying.