Question

JSON to JSON LD

  • 23 December 2019
  • 13 replies
  • 27 views

Userlevel 3
Badge +12

Hi,

 

 

I am looking for advice how to turn a JSON file into a JSON LD (linked data) with FME.

This is an example of a JSON LD format

https://json-ld.org/spec/latest/json-ld-framing/#example-3-flattened-library-objects

There are 2 parts:

1) @context: where you place the link to the complete vocabulary of the model

2) @graph: where the features are written.

How can I introduce these 2 parts when writing JSON in FME?

Jasper


13 replies

Badge +22

I think you'll need to structure your data with a JSON templater.

The root object would contain your context information and the features would be a subtemplate.

 

Depending on your source data, you could use a creator for the root, or a sampler on you data with the 1st feaure going to both the root and the subtemplate (with a group by if you have more than 1 context in your data).
Userlevel 3
Badge +12

I experimented a little with the JSONTemplater, but i was not able to write the @context and @graph part succesfully yet.

Can you put me on the right track?

 

 

json_LD_test.fmw

Badge +22

I experimented a little with the JSONTemplater, but i was not able to write the @context and @graph part succesfully yet.

Can you put me on the right track?

 

 

json_LD_test.fmw

because the JSONTempater already structures your data, you can use a text writer rather than a json writer you output your data. Set the result attribute on the templater to text_line_data.

 

 

Presumably you want a sampler to send only one feature to the root.

Also the subtemplate should be a json object per the spec, not just an array of ids.33054-json-ld-test.fmw

Userlevel 3
Badge +12

Great. Thank you for the help.

One more question: is it possible to write all available attributes for the Sub Template at once?

The name of the attribute identical to the name in fme.

Or do I need to define it attribute by attribute?

Userlevel 3
Badge +12

because the JSONTempater already structures your data, you can use a text writer rather than a json writer you output your data. Set the result attribute on the templater to text_line_data.

 

 

Presumably you want a sampler to send only one feature to the root.

Also the subtemplate should be a json object per the spec, not just an array of ids.33054-json-ld-test.fmw

Great. Thank you for the help.

One more question: is it possible to write all available attributes for the Sub Template at once?

The name of the attribute identical to the name in fme.

Or do I need to define it attribute by attribute?

Badge +22

Great. Thank you for the help.

One more question: is it possible to write all available attributes for the Sub Template at once?

The name of the attribute identical to the name in fme.

Or do I need to define it attribute by attribute?

There may be way using xquery and for loops, but I don't know it well enough to say.

Userlevel 3
Badge +12

There may be way using xquery and for loops, but I don't know it well enough to say.

Thank you for the help jdh.

@takashi Do you have a solution for writing all attributes in the sub template without defining them individually?

Userlevel 2
Badge +17

Thank you for the help jdh.

@takashi Do you have a solution for writing all attributes in the sub template without defining them individually?

Maybe possible, but how to depends on structure of the source JSON document. Can you post a source example and your desired result that should be generated from the source?

Userlevel 3
Badge +12

Maybe possible, but how to depends on structure of the source JSON document. Can you post a source example and your desired result that should be generated from the source?

Hi Takashi,

 

It's the extension of the topic you helped me with.

https://knowledge.safe.com/questions/104798/json-to-flat-table.html

source.jsondesired_output.jsonjson_LD_workinprogress.fmw

Userlevel 2
Badge +17

Thank you for the help jdh.

@takashi Do you have a solution for writing all attributes in the sub template without defining them individually?

Looks like the destination "@graph" array is exactly equal to the source "collection" array. Isn't it?

If my observation above was correct, it's not necessary to perform either fragmentation or flattening the JSON document at all. Just extract the "collection" array and build your desired JSON structure containing it.

Example:

{
    "@context" : "http://www.google.be/context",
    "@graph" : fme:get-json-attribute("_collection")
}

0684Q00000ArNQaQAN.png

 

This could also be available.

{
    "@context" : "http://www.google.be/context",
    "@graph" : fme:get-json-attribute("text_line_data")("collection")
}

0684Q00000ArNSFQA3.png

Userlevel 3
Badge +12

Looks like the destination "@graph" array is exactly equal to the source "collection" array. Isn't it?

If my observation above was correct, it's not necessary to perform either fragmentation or flattening the JSON document at all. Just extract the "collection" array and build your desired JSON structure containing it.

Example:

{
    "@context" : "http://www.google.be/context",
    "@graph" : fme:get-json-attribute("_collection")
}

0684Q00000ArNQaQAN.png

 

This could also be available.

{
    "@context" : "http://www.google.be/context",
    "@graph" : fme:get-json-attribute("text_line_data")("collection")
}

0684Q00000ArNSFQA3.png

Works perfectly.

I see one disadvantage for this solution, I think there is no flexibility to add or change attributes. Am I correct?

 

To get a correct JSON LD, I would like to add "@id" and "@type" later.
Userlevel 2
Badge +17

Great. Thank you for the help.

One more question: is it possible to write all available attributes for the Sub Template at once?

The name of the attribute identical to the name in fme.

Or do I need to define it attribute by attribute?

If you need to add @id and @type element to every object in the array, this workflow example might help you.

Root Template Expression:

{
    "@context" : "http://www.google.be/context",
    "@graph" : [ fme:process-features("SUB") ]
}

SUB Template Expression:

{|
    {"@id" : 1},
    {"@type" : "something"},
    fme:get-json-attribute("text_line_data")
|}

0684Q00000ArMNOQA3.png

Userlevel 2
Badge +17

Great. Thank you for the help.

One more question: is it possible to write all available attributes for the Sub Template at once?

The name of the attribute identical to the name in fme.

Or do I need to define it attribute by attribute?

How do you determine the values of @id and @type?

Reply