You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用rdflib实现JSON-LD转Turtle失败,求排查方案

JSON-LD转Turtle时rdflib解析报错的解决

问题场景

编写JSON-LD与Turtle互转脚本,Turtle转JSON-LD功能正常,但反向转换失败。使用环境:rdflib 6.3.2、Python 3.11(Windows系统)。

执行代码

from rdflib import Graph

g = Graph()

#input file name and file format here
g.parse(location=r'C:\Users\franken\PycharmProjects\rdf_tools\LD_conversion\input\test_converted.json', format='json-ld')

#output file name and file format here
g.serialize(destination=r'C:\Users\franken\PycharmProjects\rdf_tools\LD_conversion\output\testbacktoturtle.ttl', format='turtle')

报错信息

C:\Users\franken\PycharmProjects\rdf_tools\LD_conversion\input\test_converted.json does not look like a valid URI, trying to serialize this will break.
Traceback (most recent call last):
  File "C:\Users\franken\PycharmProjects\rdf_tools\LD_conversion\main.py", line 24, in <module>
    g.parse(location=r'C:\Users\franken\PycharmProjects\rdf_tools\LD_conversion\input\test_converted.json', format='json-ld')
  File "C:\Python311\Lib\site-packages\rdflib\graph.py", line 1470, in parse
    source = create_input_source(
             ^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\rdflib\parser.py", line 416, in create_input_source
    ) = _create_input_source_from_location(
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\rdflib\parser.py", line 478, in _create_input_source_from_location
    input_source = URLInputSource(absolute_location, format)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\rdflib\parser.py", line 285, in __init__
    response: addinfourl = _urlopen(req)
                           ^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\rdflib\parser.py", line 272, in _urlopen
    return urlopen(req)
           ^^^^^^^^^^^^
  File "C:\Python311\Lib\urllib\request.py", line 216, in urlopen
    return opener.open(url, data, timeout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\urllib\request.py", line 519, in open
    response = self._open(req, data)
               ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\urllib\request.py", line 541, in _open
    return self._call_chain(self.handle_open, 'unknown',
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\urllib\request.py", line 496, in _call_chain
    result = func(*args)
             ^^^^^^^^^^^
  File "C:\Python311\Lib\urllib\request.py", line 1419, in unknown_open
    raise URLError('unknown url type: %s' % type)
urllib.error.URLError: <urlopen error unknown url type: c>

Process finished with exit code 1

输入的JSON-LD文件内容

{
  "@context": {
    "@context": {
      "@bio": "<https://bioschemas.org/>",
      "@ex:": "<http://example.com/ns#>",
      "@owl": "<http://www.w3.org/2002/07/owl#>",
      "@qudt": "<http://qudt.org/schema/qudt/>",
      "@rdf": "<http://www.w3.org/1999/02/22-rdf-syntax-ns#>",
      "@rdfs": "<http://www.w3.org/2000/01/rdf-schema#>",
      "@schema": "<http://schema.org/>",
      "@sd": "<https://w3id.org/okn/o/sd#>",
      "@sh": "<http://www.w3.org/ns/shacl#>",
      "@skos": "<http://www.w3.org/2004/02/skos/core#>",
      "@spe": "<https://openschemas.github.io/spec-container/specifications/>",
      "@xsd": "<http://www.w3.org/2001/XMLSchema#>"
    }
  },
  "@id": "https://www.blabla.com/SUBJECT_AREA",
  "@type": "https://blabla.px_variable.com",
  "http://www.w3.org/2004/02/skos/core#prefLabel": {
    "@language": "en",
    "@value": "SUBJECT AREA"
  },
  "http://www.yourprefix.ch/isDefinedAs": [
    {
      "@language": "en",
      "@value": "A definition of something, in english in this case."
    },
    {
      "@language": "fr",
      "@value": "Une definition de quelquechose"
    },
    {
      "@language": "it",
      "@value": "Una “definizione di qualcosa"
    },
    {
      "@language": "de",
      "@value": "Eine Definition von etwas"
    }
  ]
}

解决建议

1. 修复本地路径的URI格式

rdflib的location参数要求本地文件路径为标准file URI格式,需添加file:///前缀,并将Windows盘符的冒号改为正斜杠:

g.parse(location=r'file:///C:/Users/franken/PycharmProjects/rdf_tools/LD_conversion/input/test_converted.json', format='json-ld')

也可以用pathlib自动处理路径转URI:

from pathlib import Path
file_path = Path(r'C:\Users\franken\PycharmProjects\rdf_tools\LD_conversion\input\test_converted.json')
g.parse(location=file_path.as_uri(), format='json-ld')

2. 改用文件对象解析(更稳定)

直接打开文件作为数据源,绕过URI解析逻辑:

with open(r'C:\Users\franken\PycharmProjects\rdf_tools\LD_conversion\input\test_converted.json', 'r', encoding='utf-8') as f:
    g.parse(f, format='json-ld')

3. 修复JSON-LD上下文格式

当前JSON-LD存在嵌套的@context,不符合规范,会导致解析异常。正确格式应去掉外层嵌套,且前缀无需加@、URI无需用<>包裹:

{
  "@context": {
    "bio": "https://bioschemas.org/",
    "ex": "http://example.com/ns#",
    "owl": "http://www.w3.org/2002/07/owl#",
    "qudt": "http://qudt.org/schema/qudt/",
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "rdfs": "http://www.w3.org/2000/01/rdf-schema#",
    "schema": "http://schema.org/",
    "sd": "https://w3id.org/okn/o/sd#",
    "sh": "http://www.w3.org/ns/shacl#",
    "skos": "http://www.w3.org/2004/02/skos/core#",
    "spe": "https://openschemas.github.io/spec-container/specifications/",
    "xsd": "http://www.w3.org/2001/XMLSchema#"
  },
  "@id": "https://www.blabla.com/SUBJECT_AREA",
  "@type": "https://blabla.px_variable.com",
  "skos:prefLabel": {
    "@language": "en",
    "@value": "SUBJECT AREA"
  },
  "http://www.yourprefix.ch/isDefinedAs": [
    {
      "@language": "en",
      "@value": "A definition of something, in english in this case."
    },
    {
      "@language": "fr",
      "@value": "Une definition de quelquechose"
    },
    {
      "@language": "it",
      "@value": "Una “definizione di qualcosa"
    },
    {
      "@language": "de",
      "@value": "Eine Definition von etwas"
    }
  ]
}

4. 验证JSON-LD有效性

先确认JSON-LD文件无语法错误,再进行解析操作。


内容的提问来源于stack exchange,提问作者Robin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 06:47:57