Невозможно получить доступ к данным из объекта json

Я сохранил структуру данных json в кадре данных с одним столбцом с именем json_data в R, поэтому мой текст json хранится в этом формате

row 1)    { "results" : [ { "formatted_address" : "Sahibzada Ajit Singh Nagar, 
          Punjab, India", "types" : [ "route" ], "location" :"lat"31.1471305,"lng" 
          75.34121789999999 }, ]
row 2)     { "results" : [ { "formatted_address" : "SAS Nagar,  Chennai, India", 
       "types" : [ "route 2" ], "location" :"lat"30.67249,"lng" 23.988672537 ,}] 
row 3)   { "results" : [ { "formatted_address" : "Hind mata chowk, UP, India", 
       "types" : [ "route 4" ], "location" lat"48.973478983,"lng"64.982486763},] 
row 4)
row 5)
.
.
.
.row 1000)

Чего я хочу добиться, так это получить доступ к адресам, отформатированным по строкам, широте и долготе, из местоположения и сохранить их в трех разных столбцах, чтобы

formatted_add                                       lat            lng
Sahibzada Ajit Singh Nagar,Punjab, India      31.1471305       75.34121789999999
SAS Nagar,  Chennai, India                    30.67249         23.988672537
Hind mata chowk, UP, India                    48.973478983     64.982486763

json_data_df - имя фрейма данных

Я пытаюсь сделать

formatted_add=json_data_df$json_data$formatted_address
lat=json_data_df$json_data$formatted_address$geometry$location$lat
lng=json_data_df$json_data$formatted_address$geometry$location$lng

пожалуйста, посмотрите O/P dput(), который я вставил в образец строки, из этой строки я хочу получить доступ к первому появлению "formatted_address" и его "lat","lng" в местоположении. Но это не дает мне вывода, никаких предложений о том, какие изменения должны быть сделаны. заранее спасибо

редактировать:

dput (головка (json_data_df, 1))

O \ P

    {
        "results": [{
            "address_components": [{
                "long_name": "Unnamed Road",
                "short_name": "Unnamed Road",
                "types": ["route"]
            }, {
                "long_name": "Sahibzada Ajit Singh Nagar",
                "short_name": "SAS Nagar",
                "types": ["administrative_area_level_2", "political"]
            }, {
                "long_name": "Punjab",
                "short_name": "PB",
                "types": ["administrative_area_level_1", "political"]
            }, {
                "long_name": "India",
                "short_name": "IN",
                "types": ["country", "political"]
            }, {
                "long_name": "160003",
                "short_name": "160003",
                "types": ["postal_code"]
            }],
            "formatted_address": "Unnamed Road, Punjab 160003, India",
            "geometry": {
                "bounds": {
                    "northeast": {
                        "lat": 30.6593213,
                        "lng": 76.7952105
                    },
                    "southwest": {
                        "lat": 30.6548837,
                        "lng": 76.7931289
                    }
                },
                "location": {
                    "lat": 30.6576094,
                    "lng": 76.794392
                },
                "location_type": "GEOMETRIC_CENTER",
                "viewport": {
                    "northeast": {
                        "lat": 30.6593213,
                        "lng": 76.79551868029151
                    },
                    "southwest": {
                        "lat": 30.6548837,
                        "lng": 76.7928207197085
                    }
                }
            },
            "place_id": "ChIJYWQws23rDzkRsqz31TNCwXg",
            "types": ["route"]
        }, {
            "address_components": [{
                "long_name": "Bhabat",
                "short_name": "Bhabat",
                "types": ["locality", "political"]
            }, {
                "long_name": "Sahibzada Ajit Singh Nagar",
                "short_name": "SAS Nagar",
                "types": ["administrative_area_level_2", "political"]
            }, {
                "long_name": "Punjab",
                "short_name": "PB",
                "types": ["administrative_area_level_1", "political"]
            }, {
                "long_name": "India",
                "short_name": "IN",
                "types": ["country", "political"]
            }],
            "formatted_address": "Bhabat, Punjab, India",
            "geometry": {
                "bounds": {
                    "northeast": {
                        "lat": 30.67249,
                        "lng": 76.81895999999999
                    },
                    "southwest": {
                        "lat": 30.64508,
                        "lng": 76.79106999999999
                    }
                },
                "location": {
                    "lat": 30.6575248,
                    "lng": 76.8057776
                },
                "location_type": "APPROXIMATE",
                "viewport": {
                    "northeast": {
                        "lat": 30.67249,
                        "lng": 76.81895999999999
                    },
                    "southwest": {
                        "lat": 30.64508,
                        "lng": 76.79106999999999
                    }
                }
            },
            "place_id": "ChIJN3sGJEHrDzkRwGZ1GMXBa2Q",
            "types": ["locality", "political"]
        }, {
            "address_components": [{
                "long_name": "Chandigarh Airport Area",
                "short_name": "Chandigarh Airport Area",
                "types": ["political", "sublocality", "sublocality_level_1"]
            }, {
                "long_name": "Chandigarh",
                "short_name": "Chandigarh",
                "types": ["locality", "political"]
            }, {
                "long_name": "Chandigarh",
                "short_name": "CH",
                "types": ["administrative_area_level_1", "political"]
            }, {
                "long_name": "India",
                "short_name": "IN",
                "types": ["country", "political"]
            }],
            "formatted_address": "Chandigarh Airport Area, Chandigarh, India",
            "geometry": {
                "bounds": {
                    "northeast": {
                        "lat": 30.696639,
                        "lng": 76.81389089999999
                    },
                    "southwest": {
                        "lat": 30.6568821,
                        "lng": 76.76542999999999
                    }
                },
                "location": {
                    "lat": 30.6783321,
                    "lng": 76.7884713
                },
                "location_type": "APPROXIMATE",
                "viewport": {
                    "northeast": {
                        "lat": 30.696639,
                        "lng": 76.81389089999999
                    },
                    "southwest": {
                        "lat": 30.6568821,
                        "lng": 76.76542999999999
                    }
                }
            },
            "place_id": "ChIJcb4-lILsDzkRJIQJTpsMm8w",
            "types": ["political", "sublocality", "sublocality_level_1"]
        }, {
            "address_components": [{
                "long_name": "160004",
                "short_name": "160004",
                "types": ["postal_code"]
            }, {
                "long_name": "India",
                "short_name": "IN",
                "types": ["country", "political"]
            }],
            "formatted_address": "160004, India",
            "geometry": {
                "bounds": {
                    "northeast": {
                        "lat": 30.6832516,
                        "lng": 76.8121771
                    },
                    "southwest": {
                        "lat": 30.6447985,
                        "lng": 76.7639648
                    }
                },
                "location": {
                    "lat": 30.6582706,
                    "lng": 76.7855866
                },
                "location_type": "APPROXIMATE",
                "viewport": {
                    "northeast": {
                        "lat": 30.6832516,
                        "lng": 76.8121771
                    },
                    "southwest": {
                        "lat": 30.6447985,
                        "lng": 76.7639648
                    }
                }
            },
            "place_id": "ChIJf560w6fsDzkRXLKn0s52kHM",
            "types": ["postal_code"]
        }, {
            "address_components": [{
                "long_name": "Sahibzada Ajit Singh Nagar",
                "short_name": "SAS Nagar",
                "types": ["administrative_area_level_2", "political"]
            }, {
                "long_name": "Punjab",
                "short_name": "PB",
                "types": ["administrative_area_level_1", "political"]
            }, {
                "long_name": "India",
                "short_name": "IN",
                "types": ["country", "political"]
            }],
            "formatted_address": "Sahibzada Ajit Singh Nagar, Punjab, India",
            "geometry": {
                "bounds": {
                    "northeast": {
                        "lat": 30.9369199,
                        "lng": 76.9439199
                    },
                    "southwest": {
                        "lat": 30.35902,
                        "lng": 76.52444009999999
                    }
                },
                "location": {
                    "lat": 30.6496486,
                    "lng": 76.7567368
                },
                "location_type": "APPROXIMATE",
                "viewport": {
                    "northeast": {
                        "lat": 30.9369199,
                        "lng": 76.9439199
                    },
                    "southwest": {
                        "lat": 30.35902,
                        "lng": 76.52444009999999
                    }
                }
            },
            "place_id": "ChIJOyhuuFPlDzkRzrRj0bvdZPw",
            "types": ["administrative_area_level_2", "political"]
        }, {
            "address_components": [{
                "long_name": "Punjab",
                "short_name": "PB",
                "types": ["administrative_area_level_1", "political"]
            }, {
                "long_name": "India",
                "short_name": "IN",
                "types": ["country", "political"]
            }],
            "formatted_address": "Punjab, India",
            "geometry": {
                "bounds": {
                    "northeast": {
                        "lat": 32.4994008,
                        "lng": 76.92175809999999
                    },
                    "southwest": {
                        "lat": 29.537147,
                        "lng": 73.88057999999999
                    }
                },
                "location": {
                    "lat": 31.1471305,
                    "lng": 75.34121789999999
                },
                "location_type": "APPROXIMATE",
                "viewport": {
                    "northeast": {
                        "lat": 32.4994008,
                        "lng": 76.92175809999999
                    },
                    "southwest": {
                        "lat": 29.537147,
                        "lng": 73.88057999999999
                    }
                }
            },
            "place_id": "ChIJVXOeVqpkGTkRYYijAzEmvY8",
            "types": ["administrative_area_level_1", "political"]
        }, {
            "address_components": [{
                "long_name": "India",
                "short_name": "IN",
                "types": ["country", "political"]
            }],
            "formatted_address": "India",
            "geometry": {
                "bounds": {
                    "northeast": {
                        "lat": 35.5087008,
                        "lng": 97.39535869999999
                    },
                    "southwest": {
                        "lat": 6.4626999,
                        "lng": 68.1097
                    }
                },
                "location": {
                    "lat": 20.593684,
                    "lng": 78.96288
                },
                "location_type": "APPROXIMATE",
                "viewport": {
                    "northeast": {
                        "lat": 35.5087008,
                        "lng": 97.39498069999999
                    },
                    "southwest": {
                        "lat": 6.7535159,
                        "lng": 68.16288519999999
                    }
                }
            },
            "place_id": "ChIJkbeSa_BfYzARphNChaFPjNc",
            "types": ["country", "political"]
        }],
        "status": "OK"
    }

row 2)
      {    "results" : [       {          "address_components" : [             {                "long_name" : "Unnamed Road",                "short_name" : "Unnamed Road",                "types" : [ "route" ]             },             {                "long_name" : "Godown Area",                "short_name" : "Godown Area",                "types" : [ "neighborhood", "political" ]             },             {                "long_name" : "Chandigarh Airport Area",                "short_name" : "Chandigarh Airport Area",                "types" : [ "political", "sublocality", "sublocality_level_1" ]             },             {                "long_name" : "Chandigarh",                "short_name" : "Chandigarh",                "types" : [ "locality", "political" ]             },             {                "long_name" : "Sahibzada Ajit Singh Nagar",                "short_name" : "SAS Nagar",                "types" : [ "administrative_area_level_2", "political" ]             },             {                "long_name" : "Chandigarh",                "short_name" : "CH",                "types" : [ "administrative_area_level_1", "political" ]             },             {                "long_name" : "India",                "short_name" : "IN",                "types" : [ "country", "political" ]             },             {                "long_name" : "140604",                "short_name" : "140604",                "types" : [ "postal_code" ]             }          ],          "formatted_address" : "Unnamed Road, Godown Area, Chandigarh Airport Area, Chandigarh, 140604, India",          "geometry" : {             "bounds" : {                "northeast" : {                   "lat" : 30.6617314,                   "lng" : 76.81240459999999                },                "southwest" : {                   "lat" : 30.6611368,                   "lng" : 76.8107763                }             },             "location" : {                "lat" : 30.66141159999999,                "lng" : 76.8115756             },             "location_type" : "GEOMETRIC_CENTER",             "viewport" : {                "northeast" : {                   "lat" : 30.66278308029149,                   "lng" : 76.81293943029151                },                "southwest" : {                   "lat" : 30.66008511970849,                   "lng" : 76.81024146970849                }             }          },          "place_id" : "ChIJBf4l00XrDzkRhE9fwLK7DNw",          "types" : [ "route" ]       },       {          "address_components" : [             {                "long_name" : "Bhabat",                "short_name" : "Bhabat",                "types" : [ "locality", "political" ]             },             {                "long_name" : "Sahibzada Ajit Singh Nagar",                "short_name" : "SAS Nagar",                "types" : [ "administrative_area_level_2", "political" ]             },             {                "long_name" : "Punjab",                "short_name" : "PB",                "types" : [ "administrative_area_level_1", "political" ]             },             {                "long_name" : "India",                "short_name" : "IN",                "types" : [ "country", "political" ]             }          ],          "formatted_address" : "Bhabat, Punjab, India",          "geometry" : {             "bounds" : {                "northeast" : {                   "lat" : 30.67249,                   "lng" : 76.81895999999999                },                "southwest" : {                   "lat" : 30.64508,                   "lng" : 76.79106999999999                }             },             "location" : {                "lat" : 30.6575248,                "lng" : 76.8057776             },             "location_type" : "APPROXIMATE",             "viewport" : {                "northeast" : {                   "lat" : 30.67249,                   "lng" : 76.81895999999999                },                "southwest" : {                   "lat" : 30.64508,                   "lng" : 76.79106999999999                }             }          },          "place_id" : "ChIJN3sGJEHrDzkRwGZ1GMXBa2Q",          "types" : [ "locality", "political" ]       },       {          "address_components" : [             {                "long_name" : "Chandigarh Airport Area",                "short_name" : "Chandigarh Airport Area",                "types" : [ "political", "sublocality", "sublocality_level_1" ]             },             {                "long_name" : "Chandigarh",                "short_name" : "Chandigarh",                "types" : [ "locality", "political" ]             },             {                "long_name" : "Chandigarh",                "short_name" : "CH",                "types" : [ "administrative_area_level_1", "political" ]             },             {                "long_name" : "India",                "short_name" : "IN",                "types" : [ "country", "political" ]             }          ],          "formatted_address" : "Chandigarh Airport Area, Chandigarh, India",          "geometry" : {             "bounds" : {                "northeast" : {                   "lat" : 30.696639,                   "lng" : 76.81389089999999                },                "southwest" : {                   "lat" : 30.6568821,                   "lng" : 76.76542999999999                }             },             "location" : {                "lat" : 30.6783321,                "lng" : 76.7884713             },             "location_type" : "APPROXIMATE",             "viewport" : {                "northeast" : {                   "lat" : 30.696639,                   "lng" : 76.81389089999999                },                "southwest" : {                   "lat" : 30.6568821,                   "lng" : 76.76542999999999                }             }          },          "place_id" : "ChIJcb4-lILsDzkRJIQJTpsMm8w",          "types" : [ "political", "sublocality", "sublocality_level_1" ]       },       {          "address_components" : [             {                "long_name" : "140604",                "short_name" : "140604",                "types" : [ "postal_code" ]             },             {                "long_name" : "Punjab",                "short_name" : "PB",                "types" : [ "administrative_area_level_1", "political" ]             },             {                "long_name" : "India",                "short_name" : "IN",                "types" : [ "country", "political" ]             }          ],          "formatted_address" : "Punjab 140604, India",          "geometry" : {             "bounds" : {                "northeast" : {                   "lat" : 30.6698436,                   "lng" : 76.8210886                },                "southwest" : {                   "lat" : 30.6552941,                   "lng" : 76.80184109999999                }             },             "location" : {                "lat" : 30.6625018,                "lng" : 76.8129878             },             "location_type" : "APPROXIMATE",             "viewport" : {                "northeast" : {                   "lat" : 30.6698436,                   "lng" : 76.8210886                },                "southwest" : {                   "lat" : 30.6552941,                   "lng" : 76.80184109999999                }             }          },          "place_id" : "ChIJpzrfc1DrDzkRc_qbC2Hz3tc",          "types" : [ "postal_code" ]       },       {          "address_components" : [             {                "long_name" : "Sahibzada Ajit Singh Nagar",                "short_name" : "SAS Nagar",                "types" : [ "administrative_area_level_2", "political" ]             },             {                "long_name" : "Punjab",                "short_name" : "PB",                "types" : [ "administrative_area_level_1", "political" ]             },             {                "long_name" : "India",                "short_name" : "IN",                "types" : [ "country", "political" ]             }          ],          "formatted_address" : "Sahibzada Ajit Singh Nagar, Punjab, India",          "geometry" : {             "bounds" : {                "northeast" : {                   "lat" : 30.9369199,                   "lng" : 76.9439199                },                "southwest" : {                   "lat" : 30.35902,                   "lng" : 76.52444009999999                }             },             "location" : {                "lat" : 30.6496486,                "lng" : 76.7567368             },             "location_type" : "APPROXIMATE",             "viewport" : {                "northeast" : {                   "lat" : 30.9369199,                   "lng" : 76.9439199                },                "southwest" : {                   "lat" : 30.35902,                   "lng" : 76.52444009999999                }             }          },          "place_id" : "ChIJOyhuuFPlDzkRzrRj0bvdZPw",          "types" : [ "administrative_area_level_2", "political" ]       },       {          "address_components" : [             {                "long_name" : "Punjab",                "short_name" : "PB",                "types" : [ "administrative_area_level_1", "political" ]             },             {                "long_name" : "India",                "short_name" : "IN",                "types" : [ "country", "political" ]             }          ],          "formatted_address" : "Punjab, India",          "geometry" : {             "bounds" : {                "northeast" : {                   "lat" : 32.4994008,                   "lng" : 76.92175809999999                },                "southwest" : {                   "lat" : 29.537147,                   "lng" : 73.88057999999999                }             },             "location" : {                "lat" : 31.1471305,                "lng" : 75.34121789999999             },             "location_type" : "APPROXIMATE",             "viewport" : {                "northeast" : {                   "lat" : 32.4994008,                   "lng" : 76.92175809999999                },                "southwest" : {                   "lat" : 29.537147,                   "lng" : 73.88057999999999                }             }          },          "place_id" : "ChIJVXOeVqpkGTkRYYijAzEmvY8",          "types" : [ "administrative_area_level_1", "political" ]       },       {          "address_components" : [             {                "long_name" : "India",                "short_name" : "IN",                "types" : [ "country", "political" ]             }          ],          "formatted_address" : "India",          "geometry" : {             "bounds" : {                "northeast" : {                   "lat" : 35.5087008,                   "lng" : 97.39535869999999                },                "southwest" : {                   "lat" : 6.4626999,                   "lng" : 68.1097                }             },             "location" : {                "lat" : 20.593684,                "lng" : 78.96288             },             "location_type" : "APPROXIMATE",             "viewport" : {                "northeast" : {                   "lat" : 35.5087008,                   "lng" : 97.39498069999999                },                "southwest" : {                   "lat" : 6.7535159,                   "lng" : 68.16288519999999                }             }          },          "place_id" : "ChIJkbeSa_BfYzARphNChaFPjNc",          "types" : [ "country", "political" ]       }    ],    "status" : "OK" }

2 ответа

Решение

Хорошо, после разъяснения, у вас есть строка JSON для каждой строки data.frame

Чтобы воссоздать данные, я беру одну предоставленную вами строку (строку) JSON и помещаю ее в две строки файла data.frame.

Поэтому для доступа к данным вам необходимо проанализировать каждую строку data.frame по отдельности.

Тем не менее, в вашем вопросе вы спрашиваете о "первом появлении formatted_address под местоположением ". В строке данных, которые вы дали, есть семь разных мест, которые вы можете увидеть по количеству place_id значения, и есть только один formatted_address в place_id, Поэтому просить о первом появлении не имеет смысла, так как есть только один.

library(jsonlite)

js <- '{
                 "results": [{
                       "address_components": [{
                       "long_name": "Unnamed Road",
                       "short_name": "Unnamed Road",
                       "types": ["route"]
                       }, {
                       .... <i've omitted the rest of the text> ...
                       },
                       "place_id": "ChIJkbeSa_BfYzARphNChaFPjNc",
                        "types": ["country", "political"]
                       }],
                        "status": "OK"
                       }'

json_data_df <- data.frame(json_data = c(js, js))

## if parsing a row individually you would do
# json_result <- fromJSON(as.character(json_data_df[1, "json_data"]))

## as you're parsing each row of the data.frame, you can use whatever looping 
## method you like

## you can get all the data out of the JSON and create a list obect of everything
lst <- lapply(1:nrow(json_data_df), function(x){
  fromJSON(as.character(json_data_df[x, 'json_data']))
})


## or you can subset it within the loop to just get the bits you want
lst <- lapply(1:nrow(json_data_df), function(x){
  js <- fromJSON(as.character(json_data_df[x, 'json_data']))
  data.frame(
    place_id = js$results$place_id,
    formatted_address = js$results$formatted_address,
    lat = js$results$geometry$location$lat,
    lng = js$results$geometry$location$lng
    )
})

lst
# [[1]]
#                      place_id                          formatted_address      lat      lng
# 1 ChIJYWQws23rDzkRsqz31TNCwXg         Unnamed Road, Punjab 160003, India 30.65761 76.79439
# 2 ChIJN3sGJEHrDzkRwGZ1GMXBa2Q                      Bhabat, Punjab, India 30.65752 76.80578
# 3 ChIJcb4-lILsDzkRJIQJTpsMm8w Chandigarh Airport Area, Chandigarh, India 30.67833 76.78847
# 4 ChIJf560w6fsDzkRXLKn0s52kHM                              160004, India 30.65827 76.78559
# 5 ChIJOyhuuFPlDzkRzrRj0bvdZPw  Sahibzada Ajit Singh Nagar, Punjab, India 30.64965 76.75674
# 6 ChIJVXOeVqpkGTkRYYijAzEmvY8                              Punjab, India 31.14713 75.34122
# 7 ChIJkbeSa_BfYzARphNChaFPjNc                                      India 20.59368 78.96288
# 
# [[2]]
#                      place_id                          formatted_address      lat      lng
# 1 ChIJYWQws23rDzkRsqz31TNCwXg         Unnamed Road, Punjab 160003, India 30.65761 76.79439
# 2 ChIJN3sGJEHrDzkRwGZ1GMXBa2Q                      Bhabat, Punjab, India 30.65752 76.80578
# 3 ChIJcb4-lILsDzkRJIQJTpsMm8w Chandigarh Airport Area, Chandigarh, India 30.67833 76.78847
# 4 ChIJf560w6fsDzkRXLKn0s52kHM                              160004, India 30.65827 76.78559
# 5 ChIJOyhuuFPlDzkRzrRj0bvdZPw  Sahibzada Ajit Singh Nagar, Punjab, India 30.64965 76.75674
# 6 ChIJVXOeVqpkGTkRYYijAzEmvY8                              Punjab, India 31.14713 75.34122
# 7 ChIJkbeSa_BfYzARphNChaFPjNc                                      India 20.59368 78.96288

Согласно комментариям - чтобы получить доступ только к первой записи для каждого объекта JSON, вы можете просто установить первый элемент в lapply

## or you can subset it within the loop to just get the bits you want
lst <- lapply(1:nrow(json_data_df), function(x){
  js <- fromJSON(as.character(json_data_df[x, 'json_data']))
  data.frame(
    place_id = js$results$place_id[1],
    formatted_address = js$results$formatted_address[1],
    lat = js$results$geometry$location$lat[1],
    lng = js$results$geometry$location$lng[1]
  )
})
lst

# [[1]]
#                      place_id                  formatted_address      lat      lng
# 1 ChIJYWQws23rDzkRsqz31TNCwXg Unnamed Road, Punjab 160003, India 30.65761 76.79439

# [[2]]
#                      place_id                  formatted_address      lat      lng
# 1 ChIJYWQws23rDzkRsqz31TNCwXg Unnamed Road, Punjab 160003, India 30.65761 76.79439

Вот одна возможность:

library(rjson)

json_data <- fromJSON(file = json_file)
formatted_add <- lapply(json_data$results, function(x) x$formatted_add)
lat <- lapply(json_data$results, function(x) x$geometry$location$lat)
lng <- lapply(json_data$results, function(x) x$geometry$location$lng)

data <- cbind(formatted_add, lat, lng)

Я надеюсь, что это то, что вам нужно.

Другие вопросы по тегам