从json文件中的唯一ID中提取元素

时间:2019-06-16 18:36:55

标签: json stata

我想从json文件中提取一系列元素,其中每个项目都以唯一ID列出,然后使用 community-contributed 命令将其写入Stata中的数据集中insheetjson

数据如下:

{
    "4f1ac817b819f25efd0000c8": {
        "name": "Carmen Fraga Estévez",
        "country": "es",
        "group": "epp",
        "lqdn": "https://memopol.lqdn.fr/europe/parliament/deputy_from_ep_id/1993/",
        "agw": null,
        "email": "carmen.fragaestevez@europarl.europa.eu",
        "firstname": "Carmen",
        "surname": "Fraga Estévez",
        "title": "Ms",
        "salutation": "Dear Ms Fraga Estévez,",
        "parltrack_id": 1993,
        "active": true,
        "committees": [
            {
                "id": "pech",
                "role": "Member"
            }
        ],
        "urls": {
            "rss": "http://www.europarl.europa.eu/rss/mep/carmen.fragaestevez/en.xml",
            "europarl": "http://www.europarl.europa.eu/meps/en/1993/_history.html",
            "photo": "http://www.europarl.europa.eu/mepphoto/1993.jpg"
        },
        "constituency": "es_pp"
 }
}

我想提取namecountry,如果有委员会,则提取idrole

json文件中的每个元素都有唯一的ID号(但每个元素都以4f1ac817b819f25efd0000c8开头)。

我在Stata中尝试了以下代码:

gen str240 name=""
gen str240 country=""
gen str240 id=""
gen str240 role=""

insheetjson name country id role using mep.json, col("name" "country" "id" "role")

但是,当我运行此代码时,我在Stata中得到以下错误代码:

{err: Invalid column name/selector 'name'. (Possible name candidates are: "4f1ac817b819f25efd0000c8" "4f1ac789b819f25efd000092"

然后继续列出json文件中的所有唯一ID号。

1 个答案:

答案 0 :(得分:1)

以下内容适用于您的示例:

clear
set obs 1

generate str240 name=""
generate str240 country=""
generate str240 id=""
generate str240 role=""

insheetjson name ///
            country ///
            id ///
            role ///
            using mep.json, ///
            col("4f1ac817b819f25efd0000c8:name" ///
                "4f1ac817b819f25efd0000c8:country" ///
                "4f1ac817b819f25efd0000c8:committees:1:id" ///
                "4f1ac817b819f25efd0000c8:committees:1:role")

compress

list

     +------------------------------------------------+
     |                 name   country     id     role |
     |------------------------------------------------|
  1. | Carmen Fraga Estévez        es   pech   Member |
     +------------------------------------------------+