How should I be encoding dates and times? - vega-lite

I'm converting date columns from an internal date/time format to something Vega-Lite can parse, and am uncertain what my target should be. In version 4, there was a date time definition object, that went something like this {"year": 2006, "month": "jan", "date": 1}, and I seem to remember that working. However some recent plots fail silently when I do that, and I seem to need to use ISO-8601 string formats.
What's the proper way to encode a date and time column in version 5?

You can specify a format object with a parse property to parse your dates in whatever format they're in.
https://vega.github.io/vega-lite/docs/data.html
Specific date formats can be provided (e.g., {foo: "date:'%m%d%Y'"}),
using the d3-time-format syntax.

Related

How to convert a string into json format in flink side?

I received a one digit as string format, for example, which look like 12.
What I want to do is to convert those strings into a json format and
write them to the text file in my local directory.
However, I didn't get the right solution except for those things that manually change the strings so that it looks like the json format. I think it is tedious and laborious tasks.
The completed json format will be shown as below.
{"temperature": 12}
Is there any libraries that achieve my issue?
Thanks.
Check out Gson. In particular, if you have a Java class with a single "temperature" field, then see this for how to convert to Json.

In JsonSchema, the format value should be set as "full-date" or "date"?

You may use jsonSchemaLint for testing purposes.
I have this JsonSchema, which sets format as "full-date". All Draft-6 validators (Json.Net) accepts the schema as valid.
{
"title": "MyTestSchema",
"type": "object",
"properties": {
"MyDateValue": {
"type": "string",
"format": "full-date",
"description": "We expect yyyy-MM-dd"
}
}
}
But it is unable to identify this Json object is wrong:
{
"MyDateValue": "2017-10-1"
}
When I switch the schema from "full-date" to "date" only, it works:
{
"title": "MyTestSchema",
"type": "object",
"properties": {
"MyDateValue": {
"type": "string",
"format": "date",
"description": "We expect yyyy-MM-dd"
}
}
}
Is the one on the top ("full-date") correct term as Json rules? Please refer some documentation.
The value should be date and not full-date please refer
this documentation
Following are the valid values
date-time : This SHOULD be a date in ISO 8601 format of YYYY-MM-
DDThh:mm:ssZ in UTC time. This is the recommended form of date/
timestamp.
date : This SHOULD be a date in the format of YYYY-MM-DD. It is
recommended that you use the "date-time" format instead of "date"
unless you need to transfer only the date part.
time : This SHOULD be a time in the format of hh:mm:ss. It is
recommended that you use the "date-time" format instead of "time"
unless you need to transfer only the time part.
utc-millisec : This SHOULD be the difference, measured in
milliseconds, between the specified time and midnight, 00:00 of
January 1, 1970 UTC. The value SHOULD be a number (integer or
float).
source : here
Referring to this issue on github here, turns out it was ok to create your own schema code, with the knowledge that the validators might not be able to capture it.
"It's technically always correct because format is extensible. However, user-defined formats will be ignored by validators that don't recognize them. And of course, if someone else is using a validator that they have configured to recognize the same name but treat it differently, then you will have an interoperability problem. However, with this particular value, full-date, it depends on your version of JSON Schema.
In draft-04 or draft-06, full-date is not an official format. Although anyone who adds it as a custom format and does not have it mean the same thing it means in RFC 3339 is asking for trouble, so you're probably fine using it this way even in those versions.
In the forthcoming (next week? hopefully?) draft-07, full-date is part of the reserved RFC 3339 format namespace, and if implemented, MUST be compatible with RFC 3339's definition of full-date. draft-07 also defines date as a synonym for full-date and recommends using it instead, as date is more common in the wild as far as I know.
Either way, this usage you're showing here is probably pretty safe since it matches RFC 3339, and as of draft-07 the standard does not enforce support but does enforce that, if implemented, it must behave as expected by RFC 3339."

Map time with RestKit

I've iOS project which is using RestKit 0.21.0 component responsible to get, parse and store in Core Data responses from remote server. In one of the backend JSON response I have something like that:
"response": [
{
"id": 1,
"start_time": "10:00:00",
"end_time": "14:00:00",
"name": "Object name"
},
.
.
.
]
In Model.xcdatamodeld I've defined entity with fields startTime and endTime type of Date. Generally all mappings JSON response to objects works correctly, but I have problem with JSON fields start_time and end_time.
do you have any advices how could be done correctly mapping time fields to data which could be stored in Core Data (SQLite datatbase)?
Create an NSDateFormatter with the appropriate format to parse your time strings. Add the date formatter with [[RKValueTransformer defaultValueTransformer] insertValueTransformer:dateFormatter atIndex:0];. Now RestKit will search through all your defined date formatters as well as the default ones whenever it needs to map to an NSDate destination.
Have inherited an app that is making extensive use of RestKit and is at 0.21 release now and its great. Needed to add date to string conversion in YYYY-MM-DD HH:MM:SS.SSS and followed the advice above to add the required date formatter to the default compound formatters at index 0. However found calls to RKObjectMapping overrode this by adding ISO8601 formatter at index 0 for backward compatibility in +(void)initialize. Commented those lines out and I am getting the correct result. I guess it is possibly the way the app is structured, there are any number of calls to RKObjectMapping and it was not possible to add the date formatter in the right place without the change to RKObjectMapping.

JSON Not converting long numbers appropriately

I have a simple JSON where number is not getting parsed properly.
[
{
"orderNumber": 1,
"customerId": 228930314431312345,
"shoppingCartId": 22893031443137109,
"firstName": "jjj"
}
]
I tried it # http://www.utilities-online.info/xmltojson/ and the result was
<?xml version="1.0" encoding="UTF-8" ?>
<orderNumber>1</orderNumber>
<customerId>228930314431312350</customerId>
<shoppingCartId>22893031443137108</shoppingCartId>
<firstName>jjj</firstName>
As you can see....the XML is different from JSON. I'm new to JSON. Am I missing something?
This is a Javascript precision problem.
According to Mozilla Developer Network:
ECMA-262 only requires a precision of up to 21 significant digits. Other implementations may not support precisions higher than required by the standard.
Source: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Number/toPrecision
I pasted your array into Google Chrome's Javascript console and got back this:
So it looks like Javascript is rounding the values before they are being converted to XML. Since your conversion is being done via Javascript in the browser at http://www.utilities-online.info/xmltojson/, it makes sense why the number was changed.
(Note: I tested on Google Chrome version 26.0.1410.43 m using Windows 7 Professional)
Edit:
Is there any reason why you cannot pass these values to Javascript as strings?
Try this:
[
{
"orderNumber": "1",
"customerId": "228930314431312345",
"shoppingCartId": "22893031443137109",
"firstName": "jjj"
}
]
I was able to do this and save the values successfully. However, you will not be able to run a math calculation on them in Javascript without losing some precision, unless you are doing something like multiplying by 0, of course.
This also converted to XML correctly using your reference http://www.utilities-online.info/xmltojson/.
Javascript represents its numbers as double precision floats which limits the largest integer number that can be represented to +-9007199254740992. Here is the ECMA documentation.

Google Charts json date format

I am using Google Charts and trying to generate my own JSON format to render the chart rather than using the libraries. All is well except for trying to figure out how to represent a date format in json that google chart will understand...
Spec:
JSON does not support JavaScript Date values (for example, "new Date(2008,1,28,0,31,26)"; the API implementation does. However, the API does now support a custom valid JSON representation of dates as a string in the following format: Date(year, month, day[,hour, minute, second[, millisecond]]) where everything after day is optional, and months are zero-based.
Reference:
https://developers.google.com/chart/interactive/docs/dev/implementing_data_source#jsondatatable
Reading the above spec would seem to indicate that having the date format represented in json as Date(year, date, month) would work but this does not appear to work for me.
Error:
Uncaught Error: Type mismatch. Value Date(2012, 10, 3) does not match type date in column index 0
json response:
{"type":"ComboChart","cols":[["date","Date"],["number","Overall"],["number","Current"],["number","Rating Count"]],"rows":[["Date(2012, 10, 3)",4.0,4.0,69],["Date(2012, 10, 4)",4.0,4.0,69]],"options":{"title":"Rating for FI","chartArea":{"width":"90%","height":"75%"},"hAxis":{"title":"Date"},"legend":"top","curveType":"none","pointSize":8,"seriesType":"bars","series":{"0":{"type":"bars","targetAxisIndex":0},"2":{"type":"line","targetAxisIndex":1}},"vAxes":{"0":{"title":"Rating","minValue":0,"maxValue":5},"1":{"title":"Rating Count"}}}}
Nothing is jumping out at me as this should be following the spec required format. What am I missing?
your syntax is wrong, i think...
you should try something like that:
{"type":"ComboChart","cols":[["date","Date"],["number","Overall"],["number","Current"],["number","Rating Count"]],"rows":[["Date(2012, 10, 3)"]],["Date(2012, 10, 4)"]],"options":{"title":"Rating for FI","chartArea":{"width":"90%","height":"75%"},"hAxis":{"title":"Date"},"legend":"top","curveType":"none","pointSize":8,"seriesType":"bars","series":{"0":{"type":"bars","targetAxisIndex":0},"2":{"type":"line","targetAxisIndex":1}},"vAxes":{"0":{"title":"Rating","minValue":0,"maxValue":5},"1":{"title":"Rating Count"}}}}
for me, this code worked:
{"c":[{"v":"Date(2012,11)"},{"v":6657}.....
but this is to change the month, not the day and using json....