RE: Type Coercion question

On Friday, April 26, 2013 1:51 AM, Rob Hoare wrote:
> I have a basic question about json-ld.  I hope this is right place to
> post it.

It definitely is.


> I have an application where I'm looking at using json-ld contexts to
> mark up documents that arrive from a variety of sources, to eventually
> compare, de-duplicate, and link items within them.  I'm confused as to
> whether Type Coercion in json-ld is intended to be helpful for this.

It probably is. Please keep in mind however that type coercion just assigns
datatypes, it does not do any type conversions or transformations at all.


> To take an oversimplified case, a date may arrive in a variety of
> formats, depending on the document source.  I need a way to put the
> dates in a standard format (ISO8601) for comparison.
> 
> If the date contains something like 2000-12-31, I can just use in the
> @context:
> "@type": "http://www.w3.org/2001/XMLSchema#date" (as an aside: what is
> type coercion supposed to do if the data isn't actually in the right
> format?).

Right. If the data isn't in the right format you will end up having
inconsistent data. Type coercion doesn't validate the values. It assumes you
know what you are doing.


> More likely, the date is in older formats like "12-31-2000", "31 Dec
> 2000", and so on.

You will probably need to "invent" your own data types in that case as they
aren't standardized. But that's not a problem, just mint a URL and use that.


> Let's say there's a web service to convert these dates, like:
> http://example.com/date/us-to-iso/{date} (where {date} is the US date,
> and the result returned is ISO8601).
> 
> Is json-ld intended to allow the IRI specified within the @type to be a
> callable service that can produce a result based on the @value of the
> field?  An IRI with a query string or parameter really.  If so, what
> would be the syntax in this case?

No, it is not. The IRI in @type is an identifier. It specifies the type
associated to the value. That being said, you can of course create a simple
map

Datatype IRI -> service URI template

Which allows you to achieve what you describe.


> If not, is anybody aware of a json-ld (or just json) compatible method
> or standard for specifying (and running) data transformations that use
> APIs within a json document?  Something like :
> "ISOdate":"http://example.com/date/us-to-iso/'Date'" (where 'Date'
> contains the value of the key with that name).

No, I'm not aware of anything that you could use out of the box. But writing
such a transformation would be quite simple. Just expand your JSON-LD
documents and all values will have the form { @value, [ @type | @language }.
So you just walk over your JSON tree and whenever you find an object having
both an @value and @type member, you would look in your datatype/service map
and run the transformation.

It might also make sense to have a look at Antonio's JSON-LD macros:
https://github.com/antoniogarrote/json-ld-macros

AFAICT they are not intended for the transformations that you need but might
provide some code that you could reuse.


> This isn't just for dates, it would apply to any incoming field that
> needs to be transformed or produce derived fields, for example calling
> a
> geocode service with a placename to generate co-ordinates. It would be
> useful to specify as much of this as possible within a @context file
> (or
> similar) for each incoming data source, rather than embedding it in
> code.
> 
> Any suggestions for the best approach to use would be helpful.

I would suggest you to start with the following approach:

1) Define a datatype-transformation map (transformation be a simple function
to begin with)

2) Define the context's for your data sources

3) If you map all properties to URLs, the simplest thing is to expand the
incoming data using the context (this transforms the data to a form that it
easy to manipulate). If you don't wanna map all properties to URLs, just use
the context as a simple lookup table.

4) Walk over the whole JSON document and either look for @value-@type pairs
(if you expanded the doc) or for properties that have an entry in your
context. Then invoke the function associated to the datatype to transform
the value.


This is a very interesting application of JSON-LD. Please keep me posted or
send me a link where I could follow your progress.


Cheers,
Markus



--
Markus Lanthaler
@markuslanthaler

Received on Sunday, 28 April 2013 08:42:17 UTC