Library for resolving JSON-Ref and JSON-LD style object references. Based on System.Text.Json.
#r "nuget: JsonRef.NET"
open JsonRef.NET
JsonRefResolver.resolve jsonString
JSON-LD supports references to objects with a reserved key @id which can be used either as an object identifier or a reference to an object identifier. The value of @id must be a string. E.g. The following object:
{
"@id" : "TheID",
"value" : "MyValue"
}
might be referenced in another part of the json file as follows:
{
"@id" : "TheID"
}
This kind of reference can be resolved with a single function call, either with a serialized json string
as input or on a deserialized System.Text.Json JsonNode
.
E.g. the following json object based on the example above:
let simpleJson =
"""
{
"source": {
"@id" : "TheID",
"value" : "MyValue"
},
"target": {
"@id" : "TheID"
}
}
"""
can be resolved as follows:
#r "nuget: JsonRef.NET"
open JsonRef.NET
JsonRefResolver.resolve simpleJson
resulting in:
{
"source": {
"@id" : "TheID",
"value" : "MyValue"
},
"target": {
"@id" : "TheID",
"value" : "MyValue"
}
}
or alternatively working on a System.Text.Json
JsonNode
:
#r "nuget: JsonRef.NET"
open JsonRef.NET
open System.Text.Json
let jsonNode = Nodes.JsonObject.Parse(simpleJson)
JsonRefResolver.resolve jsonNode
Currently there is no support for autodetection of recursive references. Instead, you can give a exclusion lists for fields that should not be resolved:
#r "nuget: JsonRef.NET"
open JsonRef.NET
let options = JsonRefResolverOptions(IgnoreFields = [|"ignoreValue"|])
JsonRefResolver.resolve(jsonString,options)
If you want to insert JSON-LD objects of one json string into referencing objects of another string, you can use the following approach:
#r "nuget: JsonRef.NET"
open JsonRef.NET
open System.Text.Json
let referencejsonNode = Nodes.JsonObject.Parse(referenceJsonString)
let targetjsonNode = Nodes.JsonObject.Parse(targetJsonString)
let options = JsonRefResolverOptions.defaultOptions
let referenceObjects = JsonRefResolver.collectObjects(referencejsonNode,options)
JsonRefResolver.fillObjects(referenceObjects,targetjsonNode,options)
|> fun s -> s.ToJsonString()