Help:JSON format

From semantic-mediawiki.org
Help:JSON formatJSON format/zh-hans
JSON format
Outputs results in JSON-based serialisations.
Collection
Further Information
Provided by: Semantic MediaWiki
Added: 1.4.2
Compatibility change: Semantic MediaWiki 1.8.0
Removed: still supported
Requirements: none
Format name: json
Enabled? Indicates whether the result format is enabled by default upon installation of the respective extension. yes
Authors: James Hong Kong , Jeroen De Dauw , Fabian Howahl
Categories: export · plaintext
Table of Contents

The result format json is used to produce links to JSON files. The export syntax has changed starting with Semantic MediaWiki 1.8.0Released on 2 December 2012 and compatible with MW 1.17.0 - 1.22.x
(except 1.22.1).
.

The change in the export syntax was necessary to ensure a higher validity among export formats, a reliance on available serialization methods, and elimination of specific error prone conversion procedures.

Semantic MediaWiki 3.0.0Released on 11 October 2018 and compatible with MW 1.27.0 - 1.31.x. introduced a set of predefined classes supporting a datatable output12 which also allows for JSON export via result format "Table"Outputs the results in a table (default for queries with printout statements)..

Parameters[edit]

General[edit]

Parameter Type Default Description
source text empty Alternative query source
limit whole number 100 The maximum number of results to return
offset whole number 0 The offset of the first result
link text all Show values as links
sort list of texts empty Property to sort the query by
order list of texts empty Order of the query sort
headers text show Display the headers/property names
mainlabel text no The label to give to the main page name
intro text empty The text to display before the query results, if there are any
outro text empty The text to display after the query results, if there are any
searchlabel text JSON Text for continuing the search
default text empty The text to display if there are no query results

Format specific[edit]

Parameter Type Default Description
type text full Serialization type
prettyprint yes/no empty A pretty-print output that displays additional indents and newlines
unescape yes/no empty Output to contain unescaped slashes and multibyte Unicode characters
filename text result.json The name for the output file

Available values for parameters:

  • type: full (default) and simple
Semantic MediaWiki 3.0.0Released on 11 October 2018 and compatible with MW 1.27.0 - 1.31.x. brought the "type"3 parameter.

Examples[edit]

Default[edit]

Syntax
{{#ask:
 [[Category:City]]
 [[Located in::Germany]] 
 |?Population
 |?Area#km²=Area
 |mainlabel=City
 |format=json
}}
Result as table
CityPopulationThe number of inhabitants of some geographical place.Area
Berlin3,520,061891.85 km²344.34 sqmi <br />
Cologne1,080,394405.02 km²156.38 sqmi <br />
Frankfurt679,664248.31 km²95.87 sqmi <br />
Munich1,353,186310.43 km²119.86 sqmi <br />
Stuttgart606,588207.35 km²80.06 sqmi <br />
Würzburg126,63587.63 km²33.83 sqmi <br />
Result as json

JSON

Display of result as json (printed)
{"printrequests":[{"label":"City","key":"","redi":"","typeid":"_wpg","mode":2,"format":false},{"label":"Population","key":"Population","redi":"","typeid":"_num","mode":1,"format":""},{"label":"Area","key":"Has_area","redi":"","typeid":"_qty","mode":1,"format":"km\u00b2"}],"results":{"Demo:Berlin":{"printouts":{"Population":[3520061],"Area":[{"value":891.85,"unit":"km\u00b2"}]},"fulltext":"Demo:Berlin","fullurl":"https:\/\/www.semantic-mediawiki.org\/wiki\/Demo:Berlin","namespace":202,"exists":"1","displaytitle":"Berlin"},"Demo:Cologne":{"printouts":{"Population":[1080394],"Area":[{"value":405.02,"unit":"km\u00b2"}]},"fulltext":"Demo:Cologne","fullurl":"https:\/\/www.semantic-mediawiki.org\/wiki\/Demo:Cologne","namespace":202,"exists":"1","displaytitle":"Cologne"},"Demo:Frankfurt":{"printouts":{"Population":[679664],"Area":[{"value":248.31,"unit":"km\u00b2"}]},"fulltext":"Demo:Frankfurt","fullurl":"https:\/\/www.semantic-mediawiki.org\/wiki\/Demo:Frankfurt","namespace":202,"exists":"1","displaytitle":"Frankfurt"},"Demo:Munich":{"printouts":{"Population":[1353186],"Area":[{"value":310.43,"unit":"km\u00b2"}]},"fulltext":"Demo:Munich","fullurl":"https:\/\/www.semantic-mediawiki.org\/wiki\/Demo:Munich","namespace":202,"exists":"1","displaytitle":"Munich"},"Demo:Stuttgart":{"printouts":{"Population":[606588],"Area":[{"value":207.36,"unit":"km\u00b2"}]},"fulltext":"Demo:Stuttgart","fullurl":"https:\/\/www.semantic-mediawiki.org\/wiki\/Demo:Stuttgart","namespace":202,"exists":"1","displaytitle":"Stuttgart"},"Demo:W\u00fcrzburg":{"printouts":{"Population":[126635],"Area":[{"value":87.63,"unit":"km\u00b2"}]},"fulltext":"Demo:W\u00fcrzburg","fullurl":"https:\/\/www.semantic-mediawiki.org\/wiki\/Demo:W%C3%BCrzburg","namespace":202,"exists":"1","displaytitle":"W\u00fcrzburg"}},"serializer":"SMW\\Serializers\\QueryResultSerializer","version":2,"rows":6}

Change file name[edit]

Another file name for exporting the results can be specified by changing the searchlabel parameter:

Syntax
{{#ask:
 [[Category:City]]
 [[Located in::Germany]] 
 |?Population
 |?Area#km²=Area
 |mainlabel=City
 |format=json
 |searchlabel=example
}}
Result

example

Pretty printed output[edit]

The separator used between results can be listed too in the exported file by setting the prettyprint parameter to "yes":

Syntax
{{#ask:
 [[Category:City]]
 [[Located in::Germany]] 
 |?Population
 |?Area#km²=Area
 |mainlabel=City
 |format=json
 |prettyprint=yes
}}
Result as table
CityPopulationThe number of inhabitants of some geographical place.Area
Berlin3,520,061891.85 km²344.34 sqmi <br />
Cologne1,080,394405.02 km²156.38 sqmi <br />
Frankfurt679,664248.31 km²95.87 sqmi <br />
Munich1,353,186310.43 km²119.86 sqmi <br />
Stuttgart606,588207.35 km²80.06 sqmi <br />
Würzburg126,63587.63 km²33.83 sqmi <br />
Result as json

JSON

Display of result as json (prettyprinted)
{
    "printrequests": [
        {
            "label": "City",
            "key": "",
            "redi": "",
            "typeid": "_wpg",
            "mode": 2,
            "format": false
        },
        {
            "label": "Population",
            "key": "Population",
            "redi": "",
            "typeid": "_num",
            "mode": 1,
            "format": ""
        },
        {
            "label": "Area",
            "key": "Has_area",
            "redi": "",
            "typeid": "_qty",
            "mode": 1,
            "format": "km\u00b2"
        }
    ],
    "results": {
        "Demo:Berlin": {
            "printouts": {
                "Population": [
                    3520061
                ],
                "Area": [
                    {
                        "value": 891.85,
                        "unit": "km\u00b2"
                    }
                ]
            },
            "fulltext": "Demo:Berlin",
            "fullurl": "https:\/\/www.semantic-mediawiki.org\/wiki\/Demo:Berlin",
            "namespace": 202,
            "exists": "1",
            "displaytitle": "Berlin"
        },
        "Demo:Cologne": {
            "printouts": {
                "Population": [
                    1080394
                ],
                "Area": [
                    {
                        "value": 405.02,
                        "unit": "km\u00b2"
                    }
                ]
            },
            "fulltext": "Demo:Cologne",
            "fullurl": "https:\/\/www.semantic-mediawiki.org\/wiki\/Demo:Cologne",
            "namespace": 202,
            "exists": "1",
            "displaytitle": "Cologne"
        },
        "Demo:Frankfurt": {
            "printouts": {
                "Population": [
                    679664
                ],
                "Area": [
                    {
                        "value": 248.31,
                        "unit": "km\u00b2"
                    }
                ]
            },
            "fulltext": "Demo:Frankfurt",
            "fullurl": "https:\/\/www.semantic-mediawiki.org\/wiki\/Demo:Frankfurt",
            "namespace": 202,
            "exists": "1",
            "displaytitle": "Frankfurt"
        },
        "Demo:Munich": {
            "printouts": {
                "Population": [
                    1353186
                ],
                "Area": [
                    {
                        "value": 310.43,
                        "unit": "km\u00b2"
                    }
                ]
            },
            "fulltext": "Demo:Munich",
            "fullurl": "https:\/\/www.semantic-mediawiki.org\/wiki\/Demo:Munich",
            "namespace": 202,
            "exists": "1",
            "displaytitle": "Munich"
        },
        "Demo:Stuttgart": {
            "printouts": {
                "Population": [
                    606588
                ],
                "Area": [
                    {
                        "value": 207.36,
                        "unit": "km\u00b2"
                    }
                ]
            },
            "fulltext": "Demo:Stuttgart",
            "fullurl": "https:\/\/www.semantic-mediawiki.org\/wiki\/Demo:Stuttgart",
            "namespace": 202,
            "exists": "1",
            "displaytitle": "Stuttgart"
        },
        "Demo:W\u00fcrzburg": {
            "printouts": {
                "Population": [
                    126635
                ],
                "Area": [
                    {
                        "value": 87.63,
                        "unit": "km\u00b2"
                    }
                ]
            },
            "fulltext": "Demo:W\u00fcrzburg",
            "fullurl": "https:\/\/www.semantic-mediawiki.org\/wiki\/Demo:W%C3%BCrzburg",
            "namespace": 202,
            "exists": "1",
            "displaytitle": "W\u00fcrzburg"
        }
    },
    "serializer": "SMW\\Serializers\\QueryResultSerializer",
    "version": 2,
    "rows": 6
}

Simple output[edit]

To export a file that only contains a bare minimum of data the type parameter needs to be set to "simple". Using mainlabel=- will allow to reduce the list even further.

Syntax
{{#ask:
 [[Category:City]]
 [[Located in::Germany]] 
 |?Population
 |?Area#km²=Area
 |mainlabel=City
 |format=json
 |type=simple
 |prettyprint=yes
}}
Result as table
CityPopulationThe number of inhabitants of some geographical place.Area
Berlin3,520,061891.85 km²344.34 sqmi <br />
Cologne1,080,394405.02 km²156.38 sqmi <br />
Frankfurt679,664248.31 km²95.87 sqmi <br />
Munich1,353,186310.43 km²119.86 sqmi <br />
Stuttgart606,588207.35 km²80.06 sqmi <br />
Würzburg126,63587.63 km²33.83 sqmi <br />
Result as json (simple - prettyprinted)

JSON

Display of result as json (simple - prettyprinted)
{
    "Berlin#202##": {
        "City": [
            "Demo:Berlin"
        ],
        "Population": [
            "3520061"
        ],
        "Area": [
            "891.85 km\u00b2"
        ]
    },
    "Cologne#202##": {
        "City": [
            "Demo:Cologne"
        ],
        "Population": [
            "1080394"
        ],
        "Area": [
            "405.02 km\u00b2"
        ]
    },
    "Frankfurt#202##": {
        "City": [
            "Demo:Frankfurt"
        ],
        "Population": [
            "679664"
        ],
        "Area": [
            "248.31 km\u00b2"
        ]
    },
    "Munich#202##": {
        "City": [
            "Demo:Munich"
        ],
        "Population": [
            "1353186"
        ],
        "Area": [
            "310.43 km\u00b2"
        ]
    },
    "Stuttgart#202##": {
        "City": [
            "Demo:Stuttgart"
        ],
        "Population": [
            "606588"
        ],
        "Area": [
            "207.36 km\u00b2"
        ]
    },
    "W\u00fcrzburg#202##": {
        "City": [
            "Demo:W\u00fcrzburg"
        ],
        "Population": [
            "126635"
        ],
        "Area": [
            "87.63 km\u00b2"
        ]
    }
}


Unescaped output[edit]

To export a file that contains unescaped slashes and multibyte Unicode characters the unescape parameter needs to be set to "yes":

Syntax
{{#ask:
 [[Category:City]]
 [[Located in::Germany]] 
 |?Population
 |?Area#km²=Area
 |mainlabel=City
 |format=json
 |unescape=yes
 |prettyprint=yes
}}
Result as table
CityPopulationThe number of inhabitants of some geographical place.Area
Berlin3,520,061891.85 km²344.34 sqmi <br />
Cologne1,080,394405.02 km²156.38 sqmi <br />
Frankfurt679,664248.31 km²95.87 sqmi <br />
Munich1,353,186310.43 km²119.86 sqmi <br />
Stuttgart606,588207.35 km²80.06 sqmi <br />
Würzburg126,63587.63 km²33.83 sqmi <br />
Result as json (unescaped - prettyprinted)

JSON

Display of result as json (unescaped - prettyprinted)
{
    "printrequests": [
        {
            "label": "City",
            "key": "",
            "redi": "",
            "typeid": "_wpg",
            "mode": 2,
            "format": false
        },
        {
            "label": "Population",
            "key": "Population",
            "redi": "",
            "typeid": "_num",
            "mode": 1,
            "format": ""
        },
        {
            "label": "Area",
            "key": "Has_area",
            "redi": "",
            "typeid": "_qty",
            "mode": 1,
            "format": "km²"
        }
    ],
    "results": {
        "Demo:Berlin": {
            "printouts": {
                "Population": [
                    3520061
                ],
                "Area": [
                    {
                        "value": 891.85,
                        "unit": "km²"
                    }
                ]
            },
            "fulltext": "Demo:Berlin",
            "fullurl": "https://www.semantic-mediawiki.org/wiki/Demo:Berlin",
            "namespace": 202,
            "exists": "1",
            "displaytitle": "Berlin"
        },
        "Demo:Cologne": {
            "printouts": {
                "Population": [
                    1080394
                ],
                "Area": [
                    {
                        "value": 405.02,
                        "unit": "km²"
                    }
                ]
            },
            "fulltext": "Demo:Cologne",
            "fullurl": "https://www.semantic-mediawiki.org/wiki/Demo:Cologne",
            "namespace": 202,
            "exists": "1",
            "displaytitle": "Cologne"
        },
        "Demo:Frankfurt": {
            "printouts": {
                "Population": [
                    679664
                ],
                "Area": [
                    {
                        "value": 248.31,
                        "unit": "km²"
                    }
                ]
            },
            "fulltext": "Demo:Frankfurt",
            "fullurl": "https://www.semantic-mediawiki.org/wiki/Demo:Frankfurt",
            "namespace": 202,
            "exists": "1",
            "displaytitle": "Frankfurt"
        },
        "Demo:Munich": {
            "printouts": {
                "Population": [
                    1353186
                ],
                "Area": [
                    {
                        "value": 310.43,
                        "unit": "km²"
                    }
                ]
            },
            "fulltext": "Demo:Munich",
            "fullurl": "https://www.semantic-mediawiki.org/wiki/Demo:Munich",
            "namespace": 202,
            "exists": "1",
            "displaytitle": "Munich"
        },
        "Demo:Stuttgart": {
            "printouts": {
                "Population": [
                    606588
                ],
                "Area": [
                    {
                        "value": 207.36,
                        "unit": "km²"
                    }
                ]
            },
            "fulltext": "Demo:Stuttgart",
            "fullurl": "https://www.semantic-mediawiki.org/wiki/Demo:Stuttgart",
            "namespace": 202,
            "exists": "1",
            "displaytitle": "Stuttgart"
        },
        "Demo:Würzburg": {
            "printouts": {
                "Population": [
                    126635
                ],
                "Area": [
                    {
                        "value": 87.63,
                        "unit": "km²"
                    }
                ]
            },
            "fulltext": "Demo:Würzburg",
            "fullurl": "https://www.semantic-mediawiki.org/wiki/Demo:W%C3%BCrzburg",
            "namespace": 202,
            "exists": "1",
            "displaytitle": "Würzburg"
        }
    },
    "serializer": "SMW\\Serializers\\QueryResultSerializer",
    "version": 2,
    "rows": 6
}

More ...[edit]


References

  1. ^  Semantic MediaWiki: GitHub pull request gh:smw:2420
  2. ^  Semantic MediaWiki: GitHub pull request gh:smw:2718
  3. ^  Semantic MediaWiki: GitHub pull request gh:smw:2690