json-ld 别名未解析
json-ld alias not parsing
我正在尝试学习 json-ld,但我在使用别名时遇到了一些问题。
当我使用具有以下上下文和文档的 JSON-ld 游乐场时:
{
"@context": {
"url": "@id",
"a": "@type",
"name": "http://schema.org/name",
"schema": "http://schema.org/"
},
"url": "http://example.com/about#gregg",
"a": "schema:Person",
"name": "Gregg Kellogg"
}
这会正确压缩为:
{
"@context": "http://schema.org/",
"id": "http://example.com/about#gregg",
"type": "Person",
"name": "Gregg Kellogg"
}
然而,当我使用带有以下代码的 Python pyld 库时:
from pyld import jsonld
import json
doc = {
"url": "http://example.com/about#gregg",
"a": "schema:Person",
"name": "Gregg Kellogg"
}
context = {
"url": "@id",
"a": "@type",
"name": "http://schema.org/name",
"schema" : "http://schema.org/"
}
compacted = jsonld.compact(doc, context)
print(json.dumps(compacted, indent=2))
只打印上下文,不打印文档:
{
"@context": {
"url": "@id",
"a": "@type",
"name": "http://schema.org/name",
"schema": "http://schema.org/"
}
}
谁能解释一下为什么在我使用 pyld 库时没有应用别名以及我做错了什么?
非常感谢
两个例子不一样。 Python 示例的问题在于输入文档没有上下文。处理器首先扩展数据并将导致丢弃未知项。打印出展开后的数据就可以看出问题:
expanded = jsonld.expand(doc)
print(json.dumps(expanded, indent=2))
[]
如果在上下文中添加,然后展开,您将看到正确的数据:
from pyld import jsonld
import json
context = {
"url": "@id",
"a": "@type",
"name": "http://schema.org/name",
"schema" : "http://schema.org/"
}
doc = {
"@context": context,
"url": "http://example.com/about#gregg",
"a": "schema:Person",
"name": "Gregg Kellogg"
}
expanded = jsonld.expand(doc)
print(json.dumps(expanded, indent=2))
[
{
"@type": [
"http://schema.org/Person"
],
"http://schema.org/name": [
{
"@value": "Gregg Kellogg"
}
],
"@id": "http://example.com/about#gregg"
}
]
为了匹配您的压缩游乐场示例,您需要添加上述上下文并使用 schema.org 上下文进行压缩:
...
compacted = jsonld.compact(doc, {"@context": "http://schema.org/"})
# or use the shortcut:
# compacted = jsonld.compact(doc, "http://schema.org/")
print(json.dumps(compacted, indent=2))
{
"@context": "http://schema.org/",
"id": "http://example.com/about#gregg",
"type": "Person",
"name": "Gregg Kellogg"
}
我正在尝试学习 json-ld,但我在使用别名时遇到了一些问题。
当我使用具有以下上下文和文档的 JSON-ld 游乐场时:
{
"@context": {
"url": "@id",
"a": "@type",
"name": "http://schema.org/name",
"schema": "http://schema.org/"
},
"url": "http://example.com/about#gregg",
"a": "schema:Person",
"name": "Gregg Kellogg"
}
这会正确压缩为:
{
"@context": "http://schema.org/",
"id": "http://example.com/about#gregg",
"type": "Person",
"name": "Gregg Kellogg"
}
然而,当我使用带有以下代码的 Python pyld 库时:
from pyld import jsonld
import json
doc = {
"url": "http://example.com/about#gregg",
"a": "schema:Person",
"name": "Gregg Kellogg"
}
context = {
"url": "@id",
"a": "@type",
"name": "http://schema.org/name",
"schema" : "http://schema.org/"
}
compacted = jsonld.compact(doc, context)
print(json.dumps(compacted, indent=2))
只打印上下文,不打印文档:
{
"@context": {
"url": "@id",
"a": "@type",
"name": "http://schema.org/name",
"schema": "http://schema.org/"
}
}
谁能解释一下为什么在我使用 pyld 库时没有应用别名以及我做错了什么?
非常感谢
两个例子不一样。 Python 示例的问题在于输入文档没有上下文。处理器首先扩展数据并将导致丢弃未知项。打印出展开后的数据就可以看出问题:
expanded = jsonld.expand(doc)
print(json.dumps(expanded, indent=2))
[]
如果在上下文中添加,然后展开,您将看到正确的数据:
from pyld import jsonld
import json
context = {
"url": "@id",
"a": "@type",
"name": "http://schema.org/name",
"schema" : "http://schema.org/"
}
doc = {
"@context": context,
"url": "http://example.com/about#gregg",
"a": "schema:Person",
"name": "Gregg Kellogg"
}
expanded = jsonld.expand(doc)
print(json.dumps(expanded, indent=2))
[
{
"@type": [
"http://schema.org/Person"
],
"http://schema.org/name": [
{
"@value": "Gregg Kellogg"
}
],
"@id": "http://example.com/about#gregg"
}
]
为了匹配您的压缩游乐场示例,您需要添加上述上下文并使用 schema.org 上下文进行压缩:
...
compacted = jsonld.compact(doc, {"@context": "http://schema.org/"})
# or use the shortcut:
# compacted = jsonld.compact(doc, "http://schema.org/")
print(json.dumps(compacted, indent=2))
{
"@context": "http://schema.org/",
"id": "http://example.com/about#gregg",
"type": "Person",
"name": "Gregg Kellogg"
}