Visitar URL original
Using org.json and jsonld-java · Issue #213 · jsonld-java/jsonld-java · GitHub
Skip to content

Using org.json and jsonld-java #213

Open

Description

@RaulGF92

Hi everyone, i tried make a JSON-LD entity using the org.json package for help me, and the bug appear when try inicializate the JsonUtils of JSON-LD using the method fromString() and the toString() of JSONObject.

When i try mix the context body,the json object and the options in the compact function, the function skip some variables that exist in the body before i call the function.

I put next of this, the example i use to try this fail.

	public static void main(String[] args) throws JsonGenerationException, IOException, JsonLdError {
		
		JSONObject json= jsonBasic2();
		JSONObject con = new JSONObject("{ \"@context\": [\"https://w3c.github.io/wot/w3c-wot-td-context.jsonld\",\n" + 
				"               \"https://w3c.github.io/wot/w3c-wot-common-context.jsonld\"]}");
		
		Map context = con.toMap();
		
		Object obj = JsonUtils.fromString(json.toString());
		
		JsonLdOptions options = new JsonLdOptions();
		
		Object compact = JsonLdProcessor.compact(obj, context, options);
		
		System.out.println(JsonUtils.toPrettyString(compact));
	}

	private static JSONObject jsonBasic2() {
		// TODO Auto-generated method stub
		JSONObject obj= new JSONObject();
		
		obj.put("name", "MyTemperatureThing");
		obj.put("@type", new JSONArray()
				.put("Thing"));
		
		obj.put("base","coap:///www.example.com:5683/temp/");
		
		return obj;
	}

Expected output:

{
  "@context": ["http://w3c.github.io/wot/w3c-wot-td-context.jsonld"],
  "@type": ["Thing"],
  "name": "MyTemperatureThing"
}

Real output:

{
  "@type" : "Thing",
  "@context" : [ "https://w3c.github.io/wot/w3c-wot-td-context.jsonld", "https://w3c.github.io/wot/w3c-wot-common-context.jsonld" ]
}

Activity

  1. anandp504 commented on Apr 16, 2018

    @anandp504

    @RaulGF92 did you get any resolution on this issue? It looks like it drops anything other than @type field and hence the JSON-LD is not as expected.

  2. ansell commented on Apr 16, 2018

    @ansell
    Member

    No, I haven't had a chance to look into what the Map created by JSONObject.toMap() looks like to determine why it might be causing an issue. Workaround may be to serialise to a JSON string instead of JSONObject.toMap()

  3. anandp504 commented on Apr 17, 2018

    @anandp504

    @ansell I think the output JSON-LD doesn't contain anything other than @type field. Please find an input json file and the context that needs to be applied to the input json. The following code is very similar to the one from the Readme.MD

    Object jsonObject = JsonUtils.fromInputStream(JsonLdConverter.class.getClassLoader().getResourceAsStream("school.json"));
    Map<String, String> uriNamespace = new HashMap<>();
    uriNamespace.put("@vocab", "http://example.com/voc/teacher/1.0.0/");
    uriNamespace.put("xsd", "http://www.w3.org/2001/XMLSchema#");
    Map<String, Object> context = new HashMap<>();
    context.put(JsonLdConsts.CONTEXT, uriNamespace);
    JsonLdOptions options = new JsonLdOptions();
    Object compact = JsonLdProcessor.compact(jsonObject, context, options);
    System.out.println(JsonUtils.toPrettyString(compact));
    

    Input json file:

    {
      "academicCalendarYearEnd": "2018",
      "academicCalendarYearStart": "2017",
      "assemblyConstituency": "Gurgaon",
      "@type": "School",
      "schoolName": "Bluebells",
      "udiseNumber": "9876",
      "clusterResourceCentre": "some Cluster Resource",
      "revenueBlock": "some block",
      "address": {
        "district": "Gurgaon",
        "type": "IndianUrbanPostalAddress",
        "mohalla": "Sector 14",
        "municipality": "MCG",
        "city": "Gurgaon",
        "pinCode": "122001",
        "wardNumber": "456"
      },
      "area": {
        "@id": "AreaTypeCode-URBAN"
      }
    }
    

    context json:

    {
      "@context":{
        "xsd":"http://www.w3.org/2001/XMLSchema#",
        "@vocab":"http://example.com/voc/teacher/1.0.0/"
      }
    }
    

    output json:

    {
      "@type" : "School",
      "@context" : {
        "@vocab" : "http://example.com/voc/teacher/1.0.0/",
        "xsd" : "http://www.w3.org/2001/XMLSchema#"
      }
    }
    
  4. ansell commented on Apr 17, 2018

    @ansell
    Member

    @anandp504 It doesn't look like you are using the org.json library in your example. Do you have a different issue?

    (The Input json file isn't valid syntax right now, is it missing an opening brace }?)

  5. anandp504 commented on Apr 17, 2018

    @anandp504

    @ansell Yes, I am not using org.json in my example. I wanted to generate a json-ld output from a json input with a given context. I have updated the input json file with an opening brace. Do all properties in the JSON need to be a IRI? Can you please let me know if I am missing something else?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions