jsonrdfsemantic-webjson-ld

Remove extra parameters from framed JSON-LD


So, lets consider the following data fetched from db:

[
  {
    "@id": "http://example.com/1",
    "http://example.com/label": "Parent",
    "http://example.com/status": "Active",
    "http://example.com/children": [
      {
        "@id": "http://example.com/2"
      }
    ]
  },
  {
    "@id": "http://example.com/2",
    "http://example.com/label": "Child",
    "http://example.com/status": "Active"
  }
]

And the frame:

{
  "@context": {
    "@base": "http://example.com/",
    "@vocab": "http://example.com/"
  },
  "@graph": {
    "status":{}
  }
}

The result will look like:

{
  "@context": {
    "@base": "http://example.com/",
    "@vocab": "http://example.com/"
  },
  "@graph": [
    {
      "@id": "1",
      "children": {
        "@id": "2",
        "label": "Child",
        "status": "Active"
      },
      "label": "Parent",
      "status": "Active"
    },
    {
      "@id": "2",
      "label": "Child",
      "status": "Active"
    }
  ]
}

As you can see in the first object, in the children section I get some extra parameters in addition to id.

Is there a way I could simplify the children list to just contain ids:

"children": [
    "2"
]

I tried adding this to my frame:

"children": {
  "@id": "http://example.com/children",
  "@type": "@id"
}

But it doesn't work as I expect.


Solution

  • Use framing flags: "@embed": "@never" or "@explicit": true.

    {
      "@context": {
        "@base": "http://example.com/",
        "@vocab": "http://example.com/"
      },
      "@graph": {
        "status": {},
        "@embed": "@never"
      }
    }
    

    or

    {
      "@context": {
        "@base": "http://example.com/",
        "@vocab": "http://example.com/"
      },
      "@graph": {
        "status": {},
        "children": {"@explicit": true, "@omitDefault": true}
      }
    }
    

    But perhaps all you need is compaction.

    If you don't want to compact arrays, toggle the respective option. In JSONLD-Java:

    final JsonLdOptions options = new JsonLdOptions();
    options.setCompactArrays(false);
    

    Playground: 1, 2, 3.