在R中重新格式化JSON文件

在R中重新格式化JSON文件,r,json,api,R,Json,Api,我有一个JSON文件,格式如下: [ { "StreetAddress": "", "City": "", "State": "", "Zip": "V6A 2P3", "County": "", "Country": "", "SPLC": "", "CountryPostalFilter": "", "AbbreviationFormat": "", "CountryAbbreviation": "" }

我有一个JSON文件,格式如下:

[
  {
    "StreetAddress": "",
    "City": "",
    "State": "",
    "Zip": "V6A 2P3",
    "County": "",
    "Country": "",
    "SPLC": "",
    "CountryPostalFilter": "",
    "AbbreviationFormat": "",
    "CountryAbbreviation": ""
  },
  {
    "StreetAddress": "",
    "City": "",
    "State": "",
    "Zip": "V6A 2P3",
    "County": "",
    "Country": "",
    "SPLC": "",
    "CountryPostalFilter": "",
    "AbbreviationFormat": "",
    "CountryAbbreviation": ""
  }
]
{
  "Locations": [
    {
      "Address": {
        "StreetAddress": "1000 Herrontown Rd",
        "City": "Princeton",
        "State": "NJ",
        "Zip": "",
        "County": "",
        "Country": null,
        "SPLC": "",
        "CountryPostalFilter": 0,
        "AbbreviationFormat": 0,
        "CountryAbbreviation": "US"
      }
    },
    {
      "Address": {
        "StreetAddress": "457 N Harrison St",
        "City": "",
        "State": "",
        "Zip": "08540",
        "County": "",
        "Country": null,
        "SPLC": "",
        "CountryPostalFilter": 0,
        "AbbreviationFormat": 0
      }
     }
  ]
}
是否有R脚本可用于按如下方式重新格式化文件:

[
  {
    "StreetAddress": "",
    "City": "",
    "State": "",
    "Zip": "V6A 2P3",
    "County": "",
    "Country": "",
    "SPLC": "",
    "CountryPostalFilter": "",
    "AbbreviationFormat": "",
    "CountryAbbreviation": ""
  },
  {
    "StreetAddress": "",
    "City": "",
    "State": "",
    "Zip": "V6A 2P3",
    "County": "",
    "Country": "",
    "SPLC": "",
    "CountryPostalFilter": "",
    "AbbreviationFormat": "",
    "CountryAbbreviation": ""
  }
]
{
  "Locations": [
    {
      "Address": {
        "StreetAddress": "1000 Herrontown Rd",
        "City": "Princeton",
        "State": "NJ",
        "Zip": "",
        "County": "",
        "Country": null,
        "SPLC": "",
        "CountryPostalFilter": 0,
        "AbbreviationFormat": 0,
        "CountryAbbreviation": "US"
      }
    },
    {
      "Address": {
        "StreetAddress": "457 N Harrison St",
        "City": "",
        "State": "",
        "Zip": "08540",
        "County": "",
        "Country": null,
        "SPLC": "",
        "CountryPostalFilter": 0,
        "AbbreviationFormat": 0
      }
     }
  ]
}
df%
列表(位置=)%>%
jsonlite::toJSON(pretty=T,auto_unbox=T)
我想这正好再现了你想要的输出

df <- jsonlite::fromJSON('[
  {
    "StreetAddress": "",
    "City": "",
    "State": "",
    "Zip": "V6A 2P3",
    "County": "",
    "Country": "",
    "SPLC": "",
    "CountryPostalFilter": "",
    "AbbreviationFormat": "",
    "CountryAbbreviation": ""
  },
  {
    "StreetAddress": "",
    "City": "",
    "State": "",
    "Zip": "V6A 2P3",
    "County": "",
    "Country": "",
    "SPLC": "",
    "CountryPostalFilter": "",
    "AbbreviationFormat": "",
    "CountryAbbreviation": ""
  }
]')
Edit:即使我设置了
simplifiedataframe=F
,该方法仍然有效,因为
tibble()
也允许将命名列表作为列


输出

{
  "Locations": [
    {
      "Address": {
        "StreetAddress": "",
        "City": "",
        "State": "",
        "Zip": "V6A 2P3",
        "County": "",
        "Country": "",
        "SPLC": "",
        "CountryPostalFilter": "",
        "AbbreviationFormat": "",
        "CountryAbbreviation": ""
      }
    },
    {
      "Address": {
        "StreetAddress": "",
        "City": "",
        "State": "",
        "Zip": "V6A 2P3",
        "County": "",
        "Country": "",
        "SPLC": "",
        "CountryPostalFilter": "",
        "AbbreviationFormat": "",
        "CountryAbbreviation": ""
      }
    }
  ]
} 

这种方法更简洁、更出色。您的方法甚至可以与
simplifiedataframe=F
一起使用,因为您也可以将命名列表作为TIBLES中的列。@Brian说得好!我没注意到。谢谢你的评论。我想我离这里越来越近了。但是,我现在的输出如下:@dkent您是否使用与上面相同的数据和代码?或者你改变了什么?