Help:JSON format

From Linux Web Expert

Revision as of 15:49, 16 December 2018 by >Kghbln (chg)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

JSON format
Outputs results in JSON-based serialisations.
Collection
Further Information
Provided by: Semantic MediaWiki
Added: 1.4.2
Compatibility change: 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 an unknown date unknown versions of MediaWiki.

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 an unknown date unknown versions of MediaWiki 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

General

⧼validator-describe-header-parameter⧽ ⧼validator-describe-header-type⧽ ⧼validator-describe-header-default⧽ ⧼validator-describe-header-description⧽
source ⧼validator-type-string⧽ ⧼validator-describe-empty⧽ Alternative query source
limit ⧼validator-type-integer⧽ 100 The maximum number of results to return
offset ⧼validator-type-integer⧽ 0 The offset of the first result
link ⧼validator-type-string⧽ all Show values as links
sort ⧼validator-type-string-list⧽ ⧼validator-describe-empty⧽ Property to sort the query by
order ⧼validator-type-string-list⧽ ⧼validator-describe-empty⧽ Order of the query sort
headers ⧼validator-type-string⧽ show Display the headers/property names
mainlabel ⧼validator-type-string⧽ no The label to give to the main page name
intro ⧼validator-type-string⧽ ⧼validator-describe-empty⧽ The text to display before the query results, if there are any
outro ⧼validator-type-string⧽ ⧼validator-describe-empty⧽ The text to display after the query results, if there are any
searchlabel ⧼validator-type-string⧽ JSON Text for continuing the search
default ⧼validator-type-string⧽ ⧼validator-describe-empty⧽ The text to display if there are no query results

Format specific

⧼validator-describe-header-parameter⧽ ⧼validator-describe-header-type⧽ ⧼validator-describe-header-default⧽ ⧼validator-describe-header-description⧽
type ⧼validator-type-string⧽ full Serialization type
prettyprint ⧼validator-type-boolean⧽ ⧼validator-describe-empty⧽ A pretty-print output that displays additional indents and newlines
unescape ⧼validator-type-boolean⧽ ⧼validator-describe-empty⧽ Output to contain unescaped slashes and multibyte Unicode characters
filename ⧼validator-type-string⧽ result.json The name for the output file

Available values for parameters:

  • type: full (default) and simple
Semantic MediaWiki 3.0.0Released on an unknown date unknown versions of MediaWiki brought the "type"3 parameter.

Examples

Default

Syntax
{{#ask:
 [[Category:City]]
 [[Located in::Germany]] 
 |?Population
 |?Area#km²=Area
 |mainlabel=City
 |format=json
}}
Result as table
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

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

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
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

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
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

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
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 ...


#scite could not render a citation text for reference "gh:smw:2690" because type "pullrequest" was not assigned to a template.

References

  1. ^ gh:smw:2420 
  2. ^ gh:smw:2718 
  3. ^ gh:smw:2690