JSON

The JavaScript Object Notation is specified by ECMA.
The syntax defines three types of elements:
- objects, a set of key-value pairs, where keys are supposed to be unique;
- values, which are either strings, numbers, boolean, or the primitive 'null';
- and, arrays, which specify sequences (containing other arrays, objects, or values).

According to Facade-X model, SPARQL Anything interprets objects and arrays as containers:
- RDF properties are used to link objects to values.
- Arrays are represented by the ordered sequence component.
- Values are expressed as rdf:Literal, selecting relevant XSD datatypes from the RDFS specification: xsd:string, xsd:boolean, xsd:int, xsd:float

By default, fields with the 'null' value are ignored, but this behaviour can be controlled via json.include-null-values option.

Extensions

SPARQL Anything selects this transformer for the following file extensions:

  • json

Media types

SPARQL Anything selects this transformer for the following media types:

  • application/json
  • application/problem+json

Default implementation

Default Transformation

Data

{
    "stringArg": "stringValue",
    "intArg": 1,
    "booleanArg": true,
    "nullArg": null,
    "arr": [ 0, 1 ]
}

Located at https://sparql-anything.cc/examples/simple.json

Query

CONSTRUCT 
  { 
    ?s ?p ?o .
  }
WHERE
  { SERVICE <x-sparql-anything:location=https://sparql-anything.cc/examples/simple.json>
      { GRAPH ?g
          { ?s  ?p  ?o }
      }
  }

Facade-X RDF

PREFIX dc:     <http://purl.org/dc/elements/1.1/>
PREFIX eg:     <http://www.example.org/>
PREFIX fx:     <http://sparql.xyz/facade-x/ns/>
PREFIX ja:     <http://jena.hpl.hp.com/2005/11/Assembler#>
PREFIX owl:    <http://www.w3.org/2002/07/owl#>
PREFIX rdf:    <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs:   <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rss:    <http://purl.org/rss/1.0/>
PREFIX vcard:  <http://www.w3.org/2001/vcard-rdf/3.0#>
PREFIX whatwg: <https://html.spec.whatwg.org/#>
PREFIX xhtml:  <http://www.w3.org/1999/xhtml#>
PREFIX xsd:    <http://www.w3.org/2001/XMLSchema#>
PREFIX xyz:    <http://sparql.xyz/facade-x/data/>

[ rdf:type        fx:root;
  xyz:arr         [ rdf:_1  "0"^^xsd:int;
                    rdf:_2  "1"^^xsd:int
                  ];
  xyz:booleanArg  true;
  xyz:intArg      "1"^^xsd:int;
  xyz:stringArg   "stringValue"
] .

Options

Summary

Option name Description Valid Values Default Value
json.path One or more JsonPath expressions as filters. E.g. json.path=value or json.path.1, json.path.2, ... to add multiple expressions. The json.path option is only recommended if users need to filter a large JSON file, for example, in combination with the slice option. It will pre-process the JSON before the execution of the query. In most cases, it is easier to query the JSON using a triple pattern, as in the example described before. Any valid JsonPath (see JsonSurfer implementation)) Not set
json.literalize One or more key values as filters. E.g. json.literalize=key or json.literalize.1, json.literalize.2, ... to add multiple expressions. The json.literalize option is only recommended if users need to treat certain JSON elements as opaque string literals, for example, when using GeoJSON. Any key values present in the JSON file Not set
json.include-null-values It tells the JSON triplifier to produce triples for null values in the JSON Object/Array. By default the triplifier uses xyz:null as null value. See Issue [#564](https://github.com/SPARQL-Anything/sparql.anything/issues/564). true/false false

json.path

Description

One or more JsonPath expressions as filters. E.g. json.path=value or json.path.1, json.path.2, ... to add multiple expressions. The json.path option is only recommended if users need to filter a large JSON file, for example, in combination with the slice option. It will pre-process the JSON before the execution of the query. In most cases, it is easier to query the JSON using a triple pattern, as in the example described before.

Valid Values

Any valid JsonPath (see JsonSurfer implementation))

Default Value

Not set

Examples

Example 1

Retrieving the lists of stars of the TV Series named "Friends" and "Cougar Town".

Input
[
   {
      "name":"Friends",
      "genres":[
         "Comedy",
         "Romance"
      ],
      "language":"English",
      "status":"Ended",
      "premiered":"1994-09-22",
      "summary":"Follows the personal and professional lives of six twenty to thirty-something-year-old friends living in Manhattan.",
      "stars":[
         "Jennifer Aniston",
         "Courteney Cox",
         "Lisa Kudrow",
         "Matt LeBlanc",
         "Matthew Perry",
         "David Schwimmer"
      ]
   },
   {
      "name":"Cougar Town",
      "genres":[
         "Comedy",
         "Romance"
      ],
      "language":"English",
      "status":"Ended",
      "premiered":"2009-09-23",
      "summary":"Jules is a recently divorced mother who has to face the unkind realities of dating in a world obsessed with beauty and youth. As she becomes older, she starts discovering herself.",
      "stars":[
         "Courteney Cox",
         "David Arquette",
         "Bill Lawrence",
         "Linda Videtti Figueiredo",
         "Blake McCormick"
      ]
   }
]

https://sparql-anything.cc/example1.json

Query
PREFIX  xyz:  <http://sparql.xyz/facade-x/data/>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX  fx:   <http://sparql.xyz/facade-x/ns/>

CONSTRUCT 
  { 
    ?s ?p ?o .
  }
WHERE
  { SERVICE <x-sparql-anything:location=https://sparql-anything.cc/example1.json>
      { fx:properties
                  fx:json.path.1  "$[?(@.name==\"Friends\")].stars" ;
                  fx:json.path.2  "$[?(@.name==\"Cougar Town\")].stars" .
        ?s        ?p              ?o
      }
  }
Result
PREFIX dc:     <http://purl.org/dc/elements/1.1/>
PREFIX eg:     <http://www.example.org/>
PREFIX fx:     <http://sparql.xyz/facade-x/ns/>
PREFIX ja:     <http://jena.hpl.hp.com/2005/11/Assembler#>
PREFIX owl:    <http://www.w3.org/2002/07/owl#>
PREFIX rdf:    <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs:   <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rss:    <http://purl.org/rss/1.0/>
PREFIX vcard:  <http://www.w3.org/2001/vcard-rdf/3.0#>
PREFIX whatwg: <https://html.spec.whatwg.org/#>
PREFIX xhtml:  <http://www.w3.org/1999/xhtml#>
PREFIX xsd:    <http://www.w3.org/2001/XMLSchema#>
PREFIX xyz:    <http://sparql.xyz/facade-x/data/>

[ rdf:type  fx:root;
  rdf:_1    [ rdf:_1  "Jennifer Aniston";
              rdf:_2  "Courteney Cox";
              rdf:_3  "Lisa Kudrow";
              rdf:_4  "Matt LeBlanc";
              rdf:_5  "Matthew Perry";
              rdf:_6  "David Schwimmer"
            ];
  rdf:_2    [ rdf:_1  "Courteney Cox";
              rdf:_2  "David Arquette";
              rdf:_3  "Bill Lawrence";
              rdf:_4  "Linda Videtti Figueiredo";
              rdf:_5  "Blake McCormick"
            ]
] .
Example 2

Retrieving the language of the TV series named "Friends".

Input
[
   {
      "name":"Friends",
      "genres":[
         "Comedy",
         "Romance"
      ],
      "language":"English",
      "status":"Ended",
      "premiered":"1994-09-22",
      "summary":"Follows the personal and professional lives of six twenty to thirty-something-year-old friends living in Manhattan.",
      "stars":[
         "Jennifer Aniston",
         "Courteney Cox",
         "Lisa Kudrow",
         "Matt LeBlanc",
         "Matthew Perry",
         "David Schwimmer"
      ]
   },
   {
      "name":"Cougar Town",
      "genres":[
         "Comedy",
         "Romance"
      ],
      "language":"English",
      "status":"Ended",
      "premiered":"2009-09-23",
      "summary":"Jules is a recently divorced mother who has to face the unkind realities of dating in a world obsessed with beauty and youth. As she becomes older, she starts discovering herself.",
      "stars":[
         "Courteney Cox",
         "David Arquette",
         "Bill Lawrence",
         "Linda Videtti Figueiredo",
         "Blake McCormick"
      ]
   }
]

https://sparql-anything.cc/example1.json

Query
PREFIX  xyz:  <http://sparql.xyz/facade-x/data/>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX  fx:   <http://sparql.xyz/facade-x/ns/>

SELECT  ?language
WHERE
  { SERVICE <x-sparql-anything:location=https://sparql-anything.cc/example1.json>
      { fx:properties
                  fx:json.path  "$[?(@.name==\"Friends\")]" .
        _:b0      xyz:language  ?language
      }
  }
Result
-------------
| language  |
=============
| "English" |
-------------
Example 3

Constructing a Facade-X RDF Graph selecting only containers that match the Json Path $[?(@.name==&quot;Friends&quot;)].

Input
[
   {
      "name":"Friends",
      "genres":[
         "Comedy",
         "Romance"
      ],
      "language":"English",
      "status":"Ended",
      "premiered":"1994-09-22",
      "summary":"Follows the personal and professional lives of six twenty to thirty-something-year-old friends living in Manhattan.",
      "stars":[
         "Jennifer Aniston",
         "Courteney Cox",
         "Lisa Kudrow",
         "Matt LeBlanc",
         "Matthew Perry",
         "David Schwimmer"
      ]
   },
   {
      "name":"Cougar Town",
      "genres":[
         "Comedy",
         "Romance"
      ],
      "language":"English",
      "status":"Ended",
      "premiered":"2009-09-23",
      "summary":"Jules is a recently divorced mother who has to face the unkind realities of dating in a world obsessed with beauty and youth. As she becomes older, she starts discovering herself.",
      "stars":[
         "Courteney Cox",
         "David Arquette",
         "Bill Lawrence",
         "Linda Videtti Figueiredo",
         "Blake McCormick"
      ]
   }
]

https://sparql-anything.cc/example1.json

Query
PREFIX  xyz:  <http://sparql.xyz/facade-x/data/>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX  fx:   <http://sparql.xyz/facade-x/ns/>

CONSTRUCT 
  { 
    ?s ?p ?o .
  }
WHERE
  { SERVICE <x-sparql-anything:location=https://sparql-anything.cc/example1.json>
      { fx:properties
                  fx:json.path  "$[?(@.name==\"Friends\")]" .
        ?s        ?p            ?o
      }
  }
Result
PREFIX dc:     <http://purl.org/dc/elements/1.1/>
PREFIX eg:     <http://www.example.org/>
PREFIX fx:     <http://sparql.xyz/facade-x/ns/>
PREFIX ja:     <http://jena.hpl.hp.com/2005/11/Assembler#>
PREFIX owl:    <http://www.w3.org/2002/07/owl#>
PREFIX rdf:    <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs:   <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rss:    <http://purl.org/rss/1.0/>
PREFIX vcard:  <http://www.w3.org/2001/vcard-rdf/3.0#>
PREFIX whatwg: <https://html.spec.whatwg.org/#>
PREFIX xhtml:  <http://www.w3.org/1999/xhtml#>
PREFIX xsd:    <http://www.w3.org/2001/XMLSchema#>
PREFIX xyz:    <http://sparql.xyz/facade-x/data/>

[ rdf:type  fx:root;
  rdf:_1    [ xyz:genres     [ rdf:_1  "Comedy";
                               rdf:_2  "Romance"
                             ];
              xyz:language   "English";
              xyz:name       "Friends";
              xyz:premiered  "1994-09-22";
              xyz:stars      [ rdf:_1  "Jennifer Aniston";
                               rdf:_2  "Courteney Cox";
                               rdf:_3  "Lisa Kudrow";
                               rdf:_4  "Matt LeBlanc";
                               rdf:_5  "Matthew Perry";
                               rdf:_6  "David Schwimmer"
                             ];
              xyz:status     "Ended";
              xyz:summary    "Follows the personal and professional lives of six twenty to thirty-something-year-old friends living in Manhattan."
            ]
] .

json.literalize

Description

One or more key values as filters. E.g. json.literalize=key or json.literalize.1, json.literalize.2, ... to add multiple expressions. The json.literalize option is only recommended if users need to treat certain JSON elements as opaque string literals, for example, when using GeoJSON.

Valid Values

Any key values present in the JSON file

Default Value

Not set


json.include-null-values

Description

It tells the JSON triplifier to produce triples for null values in the JSON Object/Array. By default the triplifier uses xyz:null as null value. See Issue [#564](https://github.com/SPARQL-Anything/sparql.anything/issues/564).

Valid Values

true/false

Default Value

false

Examples

Example 1

Selecting properties having null value.

Input
{
    "stringArg": "stringValue",
    "intArg": 1,
    "booleanArg": true,
    "nullArg": null,
    "arr": [ 0, 1 ]
}

https://sparql-anything.cc/examples/simple.json

Query
PREFIX  xyz:  <http://sparql.xyz/facade-x/data/>

SELECT  ?p
WHERE
  { SERVICE <x-sparql-anything:location=https://sparql-anything.cc/examples/simple.json,json.include-null-values=true>
      { ?s  ?p  xyz:null }
  }
Result
---------------------------------------------
| p                                         |
=============================================
| <http://sparql.xyz/facade-x/data/nullArg> |
---------------------------------------------