fpotier opened a new issue, #3361:
URL: https://github.com/apache/jena/issues/3361

   ### Version
   
   5.5.0
   
   ### Question
   
   How can I retrieve the original blank node identifiers when reading a 
JSON-LD document?
   
   It appears to be possible with Turtle but not JSON-LD.
   Here is the example code I tried
   
   ```java
   import java.io.ByteArrayInputStream;
   import org.apache.jena.rdf.model.Model;
   import org.apache.jena.rdf.model.ModelFactory;
   import org.apache.jena.riot.Lang;
   import org.apache.jena.riot.RDFParser;
   import org.apache.jena.riot.lang.LabelToNode;
   import org.apache.jena.riot.out.NodeFmtLib;
   
   public class Main {
     public static void main(String[] args) {
       String jsonLdDocument =
           "{\n"
               + "  \"@context\": {\n"
               + "    \"schema\": \"http://schema.org/\"\n";
               + "  },\n"
               + "  \"@id\": \"_:person1\",\n"
               + "  \"@type\": \"schema:Person\",\n"
               + "  \"schema:name\": \"John Doe\",\n"
               + "  \"schema:birthDate\": \"1990-01-01\",\n"
               + "  \"schema:address\": {\n"
               + "    \"@type\": \"schema:PostalAddress\",\n"
               + "    \"schema:streetAddress\": \"123 Main St\",\n"
               + "    \"schema:addressLocality\": \"Anytown\",\n"
               + "    \"schema:addressRegion\": \"CA\",\n"
               + "    \"schema:postalCode\": \"12345\",\n"
               + "    \"schema:addressCountry\": \"USA\"\n"
               + "  }\n"
               + "}";
   
       String ttlDocument =
           "@prefix schema: <http://schema.org/> .\n"
               + "\n"
               + "_:person1 a schema:Person ;\n"
               + "    schema:name \"John Doe\" ;\n"
               + "    schema:birthDate \"1990-01-01\" ;\n"
               + "    schema:address [\n"
               + "        a schema:PostalAddress ;\n"
               + "        schema:streetAddress \"123 Main St\" ;\n"
               + "        schema:addressLocality \"Anytown\" ;\n"
               + "        schema:addressRegion \"CA\" ;\n"
               + "        schema:postalCode \"12345\" ;\n"
               + "        schema:addressCountry \"USA\"\n"
               + "    ] .";
   
       Model model = ModelFactory.createDefaultModel();
       RDFParser.create()
           .source(new ByteArrayInputStream(ttlDocument.getBytes()))
           .labelToNode(LabelToNode.createUseLabelEncoded())
           .lang(Lang.TTL)
           .parse(model);
       printDecodedSubjectNames(model);
   
       model = ModelFactory.createDefaultModel();
       RDFParser.create()
           .source(new ByteArrayInputStream(jsonLdDocument.getBytes()))
           .labelToNode(LabelToNode.createUseLabelEncoded())
           .lang(Lang.JSONLD11)
           .parse(model);
       printDecodedSubjectNames(model);
     }
   
     static void printDecodedSubjectNames(Model model) {
       model
           .listSubjects()
           .forEach(
               r -> {
                 
System.out.println(NodeFmtLib.decodeBNodeLabel(r.getId().getLabelString()));
               });
       ;
     }
   }
   ```
   Which outputs:
   ```console
   # When using TTL, the identifiers are correctly decoded
   person1
   genid0
   # When using JSON-LD, the identifiers are not correctly decoded
   b0
   b1
   ```
   
   After taking a quick look at the code, I believe it might be because the 
json-ld library uses has it's own blank node identifier allocation mechanism 
that doesn't seem to be reversible.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to