ReproNim / reproschema-py

Apache License 2.0
2 stars 8 forks source link

error when converting to turtle #24

Closed djarecka closed 2 years ago

djarecka commented 2 years ago

I have a jasonld, that doesn't give me any error when using json-ld playground

test_json = {
   "@context": {
      "name": "http://xmlns.com/foaf/0.1/name",
      "homepage": {
        "@id": "http://xmlns.com/foaf/0.1/homepage",
        "@type": "@id"
      }
   },
   "name": "Manu Sporny",
   "homepage": "http://manu.sporny.org/"
}

but when I'm trying to convert to turtle using to_newformat(test_json, format="turtle") it complains about @id and I get error:

---------------------------------------------------------------------------
JsonLdError                               Traceback (most recent call last)
File ~/miniconda3/envs/reproschema_sd/lib/python3.8/site-packages/pyld/jsonld.py:1219, in JsonLdProcessor.to_rdf(self, input_, options)
   1217 try:
   1218     # expand input
-> 1219     expanded = self.expand(input_, options)
   1220 except JsonLdError as cause:

File ~/miniconda3/envs/reproschema_sd/lib/python3.8/site-packages/pyld/jsonld.py:870, in JsonLdProcessor.expand(self, input_, options)
    869 # do expansion
--> 870 expanded = self._expand(active_ctx, None, document, options,
    871     inside_list=False)
    873 # optimize away @graph with no other properties

File ~/miniconda3/envs/reproschema_sd/lib/python3.8/site-packages/pyld/jsonld.py:2233, in JsonLdProcessor._expand(self, active_ctx, active_property, element, options, inside_list, inside_index, type_scoped_ctx)
   2231 for e in element:
   2232     # expand element
-> 2233     e = self._expand(
   2234         active_ctx, active_property, e, options,
   2235         inside_list=inside_list,
   2236         inside_index=inside_index,
   2237         type_scoped_ctx=type_scoped_ctx)
   2238     if inside_list and _is_array(e):

File ~/miniconda3/envs/reproschema_sd/lib/python3.8/site-packages/pyld/jsonld.py:2329, in JsonLdProcessor._expand(self, active_ctx, active_property, element, options, inside_list, inside_index, type_scoped_ctx)
   2328 rval = {}
-> 2329 self._expand_object(
   2330     active_ctx, active_property, expanded_active_property,
   2331     element, rval, options,
   2332     inside_list,
   2333     type_key,
   2334     type_scoped_ctx)
   2336 # get property count on expanded output

File ~/miniconda3/envs/reproschema_sd/lib/python3.8/site-packages/pyld/jsonld.py:2488, in JsonLdProcessor._expand_object(self, active_ctx, active_property, expanded_active_property, element, expanded_parent, options, inside_list, type_key, type_scoped_ctx)
   2487 if not options.get('isFrame'):
-> 2488     raise JsonLdError(
   2489         'Invalid JSON-LD syntax; "@id" value must be a '
   2490         'string.', 'jsonld.SyntaxError',
   2491         {'value': value}, code='invalid @id value')
   2492 if _is_object(value):

JsonLdError: ('Invalid JSON-LD syntax; "@id" value must be a string.',)
Type: jsonld.SyntaxError
Code: invalid @id value
Details: {'value': {'@context': {'name': 'http://xmlns.com/foaf/0.1/name', 'homepage': {'@id': 'http://xmlns.com/foaf/0.1/homepage', '@type': '@id'}}, 'name': 'Manu Sporny', 'homepage': 'http://manu.sporny.org/'}}

During handling of the above exception, another exception occurred:

JsonLdError                               Traceback (most recent call last)
File ~/miniconda3/envs/reproschema_sd/lib/python3.8/site-packages/pyld/jsonld.py:1128, in JsonLdProcessor.normalize(self, input_, options)
   1127         opts['produceGeneralizedRdf'] = False
-> 1128         dataset = self.to_rdf(input_, opts)
   1129 except JsonLdError as cause:

File ~/miniconda3/envs/reproschema_sd/lib/python3.8/site-packages/pyld/jsonld.py:1221, in JsonLdProcessor.to_rdf(self, input_, options)
   1220 except JsonLdError as cause:
-> 1221     raise JsonLdError(
   1222         'Could not expand input before serialization to '
   1223         'RDF.', 'jsonld.RdfError', cause=cause)
   1225 # create node map for default graph (and any named graphs)

JsonLdError: ('Could not expand input before serialization to RDF.',)
Type: jsonld.RdfError
Cause: ('Invalid JSON-LD syntax; "@id" value must be a string.',)
Type: jsonld.SyntaxError
Code: invalid @id value
Details: {'value': {'@context': {'name': 'http://xmlns.com/foaf/0.1/name', 'homepage': {'@id': 'http://xmlns.com/foaf/0.1/homepage', '@type': '@id'}}, 'name': 'Manu Sporny', 'homepage': 'http://manu.sporny.org/'}}  File "/Users/dorota/miniconda3/envs/reproschema_sd/lib/python3.8/site-packages/pyld/jsonld.py", line 1219, in to_rdf
    expanded = self.expand(input_, options)
  File "/Users/dorota/miniconda3/envs/reproschema_sd/lib/python3.8/site-packages/pyld/jsonld.py", line 870, in expand
    expanded = self._expand(active_ctx, None, document, options,
  File "/Users/dorota/miniconda3/envs/reproschema_sd/lib/python3.8/site-packages/pyld/jsonld.py", line 2233, in _expand
    e = self._expand(
  File "/Users/dorota/miniconda3/envs/reproschema_sd/lib/python3.8/site-packages/pyld/jsonld.py", line 2329, in _expand
    self._expand_object(
  File "/Users/dorota/miniconda3/envs/reproschema_sd/lib/python3.8/site-packages/pyld/jsonld.py", line 2488, in _expand_object
    raise JsonLdError(

During handling of the above exception, another exception occurred:

JsonLdError                               Traceback (most recent call last)
Input In [12], in <module>
----> 1 json_v11_ttl = to_newformat(test_json, format="turtle")

File ~/miniconda3/envs/reproschema_sd/lib/python3.8/site-packages/reproschema/jsonldutils.py:113, in to_newformat(path, format, prefixfile, contextfile)
    111     return json.dumps(data, indent=2)
    112 kwargs = {"algorithm": "URDNA2015", "format": "application/n-quads"}
--> 113 nt = jsonld.normalize(data, kwargs)
    114 if format == "n-triples":
    115     return nt

File ~/miniconda3/envs/reproschema_sd/lib/python3.8/site-packages/pyld/jsonld.py:272, in normalize(input_, options)
    247 def normalize(input_, options=None):
    248     """
    249     Performs RDF dataset normalization on the given input. The input is
    250     JSON-LD unless the 'inputFormat' option is used. The output is an RDF
   (...)
    270     :return: the normalized output.
    271     """
--> 272     return JsonLdProcessor().normalize(input_, options)

File ~/miniconda3/envs/reproschema_sd/lib/python3.8/site-packages/pyld/jsonld.py:1130, in JsonLdProcessor.normalize(self, input_, options)
   1128         dataset = self.to_rdf(input_, opts)
   1129 except JsonLdError as cause:
-> 1130     raise JsonLdError(
   1131         'Could not convert input to RDF dataset before normalization.',
   1132         'jsonld.NormalizeError', cause=cause)
   1134 # do normalization
   1135 if options['algorithm'] == 'URDNA2015':

JsonLdError: ('Could not convert input to RDF dataset before normalization.',)
Type: jsonld.NormalizeError
Cause: ('Could not expand input before serialization to RDF.',)
Type: jsonld.RdfError
Cause: ('Invalid JSON-LD syntax; "@id" value must be a string.',)
Type: jsonld.SyntaxError
Code: invalid @id value
Details: {'value': {'@context': {'name': 'http://xmlns.com/foaf/0.1/name', 'homepage': {'@id': 'http://xmlns.com/foaf/0.1/homepage', '@type': '@id'}}, 'name': 'Manu Sporny', 'homepage': 'http://manu.sporny.org/'}}  File "/Users/dorota/miniconda3/envs/reproschema_sd/lib/python3.8/site-packages/pyld/jsonld.py", line 1219, in to_rdf
    expanded = self.expand(input_, options)
  File "/Users/dorota/miniconda3/envs/reproschema_sd/lib/python3.8/site-packages/pyld/jsonld.py", line 870, in expand
    expanded = self._expand(active_ctx, None, document, options,
  File "/Users/dorota/miniconda3/envs/reproschema_sd/lib/python3.8/site-packages/pyld/jsonld.py", line 2233, in _expand
    e = self._expand(
  File "/Users/dorota/miniconda3/envs/reproschema_sd/lib/python3.8/site-packages/pyld/jsonld.py", line 2329, in _expand
    self._expand_object(
  File "/Users/dorota/miniconda3/envs/reproschema_sd/lib/python3.8/site-packages/pyld/jsonld.py", line 2488, in _expand_object
    raise JsonLdError(
  File "/Users/dorota/miniconda3/envs/reproschema_sd/lib/python3.8/site-packages/pyld/jsonld.py", line 1128, in normalize
    dataset = self.to_rdf(input_, opts)
  File "/Users/dorota/miniconda3/envs/reproschema_sd/lib/python3.8/site-packages/pyld/jsonld.py", line 1221, in to_rdf
    raise JsonLdError(

Is this a bug or there is a reason to not support it?

satra commented 2 years ago

to_newformat only accepts a path, see: https://github.com/dandi/dandi-archive/issues/1096

djarecka commented 2 years ago

that's not true, I was using this with json, e.g.:

test_json_11 = {
  "@context": {
    "@version": 1.1,
    "skos": "http://www.w3.org/2004/02/skos/core#",
    "labels": "@nest",
    "main_label": {"@id": "skos:prefLabel"},
    "other_label": {"@id": "skos:altLabel"},
    "homepage": {"@id": "http://schema.org/description", "@type": "@id"}
  },
  "@id": "http://example.org/myresource",
  "homepage": "http://example.org",
  "labels": {
     "main_label": "This is the main label for my resource",
     "other_label": "This is the other label"
  }
}
to_newformat(test_json_11, format="turtle")
satra commented 2 years ago

i pasted the wrong link, here is the correct one: https://github.com/ReproNim/reproschema-py/blob/master/reproschema/jsonldutils.py#L78

the code is set up only to accept a path or url. it may work on some json strings only when the @id is an absolute uri, as is the case of your latest example. it will not work with missing @id or implicit ones or relative ones.

djarecka commented 2 years ago

oh, ok. I used json for dandi, so assumed it should work for json. Thanks