如何使用R从JSON对象中删除命名元素

时间:2017-09-16 03:17:20

标签: json r

有人能够告诉我如何使用R从JSON对象(dt_stop_time_update)中删除命名元素(stop_sequence,stop_id,arrival_delay和departure_delay)?我的输出样本如下:

[
{
 "dt_trip_info": {
 "trip_id": [ "10034820-LCBS 17_18-LCBS_SAT-Saturday-02" ],
"start_time": [ "13:46:00" ],
"start_date": [ "20170916" ],
"route_id": [ "550-862" ] 
},
"dt_stop_time_update": {
 "stop_sequence": [      1,      2,      3,      4,      5,      6,          7,      8,      9,     10,     11,     12,     13,     14,     15,     16,         17,     18,     19 ],
"stop_id": [ "310024", "313288", "313291", "311040", "311038", "311025",     "311020", "313278", "310665", "318126", "310667", "310660", "310653",     "310655", "310658", "310656", "310610", "310617", "310615" ],
"arrival_time": [      0, 1.5055e+09, 1.5055e+09, 1.5055e+09, 1.5055e+09,     1.5055e+09, 1.5055e+09, 1.5055e+09, 1.5055e+09, 1.5055e+09, 1.5055e+09,     1.5055e+09, 1.5055e+09, 1.5055e+09, 1.5055e+09, 1.5055e+09, 1.5055e+09,     1.5055e+09, 1.5055e+09 ],
"arrival_delay": [ 0, 30, 25, 9, -12, -1, -8, -40, -16, 9, 68, 31, 35, 7,     -22, 13, 8, -35, -35 ],
"departure_time": [ 1.5055e+09, 1.5055e+09, 1.5055e+09, 1.5055e+09,     1.5055e+09, 1.5055e+09, 1.5055e+09, 1.5055e+09, 1.5055e+09, 1.5055e+09,     1.5055e+09, 1.5055e+09, 1.5055e+09, 1.5055e+09, 1.5055e+09, 1.5055e+09,     1.5055e+09, 1.5055e+09, 1.5055e+09 ],
"departure_delay": [ 0, 30, 25, 9, -12, -1, -8, -40, -16, 9, 68, 31, 35,     7, -22, 13, 8, -35, -35 ] 
} 
},
  • 我只是想补充说重复的元素序列是不均匀的。我相信输出结构是一个原子列表。

1 个答案:

答案 0 :(得分:0)

您需要将json导入列表,然后将要删除的元素设置为NULL

library(jsonlite)

dat <- read_json("test.json")

dat[[1]]$dt_stop_time_update$stop_sequence <- NULL
dat[[1]]$dt_stop_time_update$stop_id <- NULL
dat[[1]]$dt_stop_time_update$arrival_delay <- NULL
dat[[1]]$dt_stop_time_update$departure_delay <- NULL

toJSON(dat, pretty = TRUE)

# [
#   {
#     "dt_trip_info": {
#       "trip_id": [
#         ["10034820-LCBS 17_18-LCBS_SAT-Saturday-02"]
#         ],
#       "start_time": [
#         ["13:46:00"]
#         ],
#       "start_date": [
#         ["20170916"]
#         ],
#       "route_id": [
#         ["550-862"]
#         ]
#     },
#     "dt_stop_time_update": {
#       "arrival_time": [
#         [0],
#         [1505500000],
#         [1505500000],
#         [1505500000],
#         [1505500000],
#         [1505500000],
#         [1505500000],
#         [1505500000],
#         [1505500000],
#         [1505500000],
#         [1505500000],
#         [1505500000],
#         [1505500000],
#         [1505500000],
#         [1505500000],
#         [1505500000],
#         [1505500000],
#         [1505500000],
#         [1505500000]
#         ],
#       "departure_time": [
#         [1505500000],
#         [1505500000],
#         [1505500000],
#         [1505500000],
#         [1505500000],
#         [1505500000],
#         [1505500000],
#         [1505500000],
#         [1505500000],
#         [1505500000],
#         [1505500000],
#         [1505500000],
#         [1505500000],
#         [1505500000],
#         [1505500000],
#         [1505500000],
#         [1505500000],
#         [1505500000],
#         [1505500000]
#         ]
#     }
#   }
#   ]