如何轻松为 Stat-Xplore API 生成查询 JSON
- 登录https://stat-xplore.dwp.gov.uk
- 打开你想要的数据集和表
- 在右上角找到“下载表格”工具
- 在下拉框中选择“Open Data API Query (.json)”并点击“Go”
- 这将下载一个 JSON 文件,其中包含用于描述您在 Web 浏览器中查看的表的查询参数。示例如下:
{
"database" : "str:database:UC_Monthly",
"measures" : [ "str:count:UC_Monthly:V_F_UC_CASELOAD_FULL" ],
"recodes" : {
"str:field:UC_Monthly:F_UC_DATE:DATE_NAME" : {
"map" : [ [ "str:value:UC_Monthly:F_UC_DATE:DATE_NAME:C_UC_DATE:202010" ] ],
"total" : false
},
"str:field:UC_Monthly:V_F_UC_CASELOAD_FULL:EMPLOYMENT_CODE" : {
"map" : [ [ "str:value:UC_Monthly:V_F_UC_CASELOAD_FULL:EMPLOYMENT_CODE:C_UC_EMPLOYMENT:0" ], [ "str:value:UC_Monthly:V_F_UC_CASELOAD_FULL:EMPLOYMENT_CODE:C_UC_EMPLOYMENT:1" ] ],
"total" : true
},
"str:field:UC_Monthly:V_F_UC_CASELOAD_FULL:PARLC_CODE" : {
"map" : [ [ "str:value:UC_Monthly:V_F_UC_CASELOAD_FULL:PARLC_CODE:V_C_MASTERGEOG11_PARLC_TO_REGION:E14000541" ], [ "str:value:UC_Monthly:V_F_UC_CASELOAD_FULL:PARLC_CODE:V_C_MASTERGEOG11_PARLC_TO_REGION:E14000542" ], [ "str:value:UC_Monthly:V_F_UC_CASELOAD_FULL:PARLC_CODE:V_C_MASTERGEOG11_PARLC_TO_REGION:W07000041" ] ],
"total" : true
}
},
"dimensions" : [ [ "str:field:UC_Monthly:V_F_UC_CASELOAD_FULL:PARLC_CODE" ], [ "str:field:UC_Monthly:F_UC_DATE:DATE_NAME" ], [ "str:field:UC_Monthly:V_F_UC_CASELOAD_FULL:EMPLOYMENT_CODE" ] ]
}
API table endpoint docs 提供更多信息。
对 OP 问题的解释
我也在尝试使用这个 API。 (我恰好在使用 Python requests 库。)
如果我向/v1/table 端点发出请求,其中一维的类型为GROUP(而不是FIELD)
measures=["str:count:UC_Monthly:V_F_UC_CASELOAD_FULL"],
dimensions=[["str:field:UC_Monthly:F_UC_DATE:DATE_NAME"],
["str:group:UC_Monthly:X_Geography+%28residence-based%29"]
]
然后在 HTTP 响应中返回这个错误 422:
requests.exceptions.HTTPError: 422 Client Error: Unprocessable Entity for url: https://stat-xplore.dwp.gov.uk/webapi/rest/v1/table
这是响应的负载,更详细地描述了错误:
{
"message":"Unexpected schema component type 'group' for 'str:group:UC_Monthly:X_Geography+%28residence-based%29'. Expected one of type [field, valueset, udf].",
"errorType":"UNEXPECTED_SCHEMA_COMPONENT_TYPE_EXCEPTION",
"component" : "str:group:UC_Monthly:X_Geography+%28residence-based%29",
"type" : "group",
"expectedTypes" : [ "field", "valueset", "udf" ]
}
如果我理解正确,它并不是在抱怨字符串的格式,而是说只有 FIELD、VALUESET 和 UDF 对象是预期的。
如果您查看schema 端点,您可以检索有关可能的组、字段等的信息。
{
'id': 'str:group:UC_Monthly:X_Geography+%28postcode%29',
'label': 'Geography (postcode)',
'location': 'https://stat-xplore.dwp.gov.uk/webapi/rest/v1/schema/str:group:UC_Monthly:X_Geography+%2528postcode%2529',
'type': 'GROUP'
}