RDFLib / rdflib-jsonld

JSON-LD parser and serializer plugins for RDFLib
Other
283 stars 71 forks source link

Convert Json to Jsonld: Don't get the properties #46

Open aidaph opened 7 years ago

aidaph commented 7 years ago

Hi all,

I have this simple json file and i wantt to convert it to a jsonld format with the below python program[1]:

[
  {
   "Event":{
            "10924":"1"
        }
  },
  {
   "Event":"2"
  }
] 

[1]

import json
from rdflib import Graph, plugin, ConjunctiveGraph
import json, rdflib_jsonld
from rdflib.plugin import register, Serializer
register('json-ld', Serializer, 'rdflib_jsonld.serializer', 'JsonLDSerializer')

context = {
          "@context": {
            "foaf" : "http://xmlns.com/foaf/0.1/",

            "@id":"http://localhost",
            "Event": {
                "@id": "http://localhost/index.php/Event",
                "@type": "foaf:Event",
                "10924": {
                        "@id": "foaf:run",
                }
            }
        }
}
x=json.loads(open('Json_File_test.txt.Short').read())
g = ConjunctiveGraph()
g.parse(data=json.dumps(x), format='json-ld', context=context)
g.serialize("event.jsonld", format='json-ld')
g.serialize("pruebajson3.rdf")
g.serialize("pruebajson3.nt", format='nt')
print(g.serialize(format='json-ld').decode())
g.close()

But i can't get the subproperty "10924", only the "Event" property in the json-ld output:

[
  {
    "@id": "_:Nf89b993bb7974a7da878a69fe81dd442",
    "http://localhost/index.php/Event": [
      {
        "@id": "_:N6c48733f5b6d4271b760c598421bbe61"
      }
    ]
  },
  {
    "@id": "_:N7e5db391ab3b4ff6b31f1938aa14abee",
    "http://localhost/index.php/Event": [
      {
        "@type": "http://xmlns.com/foaf/0.1/Event",
        "@value": "2"
      }
    ]
  },
  {
    "@id": "_:N6c48733f5b6d4271b760c598421bbe61"
  }
]

I can't figure it out what i left in the program, do i have to create a new context for the subproperty "10924"?

Thank you so much in advance.

Kind Regards!

rodrigodesalvobraz commented 6 years ago

This is kind of late, but here it is. It seems this is happening because your context defined the attribute "Event" in its top-level but not the attribute "10924" (which was instead defined inside "Event"). You would need something like

context = {
         "@context": {
           "foaf" : "http://xmlns.com/foaf/0.1/",
           "@id":"http://localhost",
           "Event": {
               "@id": "http://localhost/index.php/Event",
               "@type": "foaf:Event",
           }
           "10924": {
                 "@id": "foaf:run",
           }
       }
})