问题
I have stored json data structure in a dataframe with single column, named json_data in R so my json text is stored in this format
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)
What I want to achieve is want to access from each row formatted address, lat, lng from location and save them into three different columns such that
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 is the name of dataframe
I'm trying to do
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
please see the O/P of dput() which I have pasted the sample row, from that row I want to access the first occurrence of "formatted_address" and its "lat","lng" under location. But this doesn't give me the output, any suggestions on what changes should be made. Thanks in advance
edit:
dput(head(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" }
回答1:
OK, after the clarification, what you have is a JSON string per each row of the data.frame
To recreate the data, I'm taking the single JSON string (row) you provided, and putting it in two rows of a data.frame
Therefore, to access the data, you need to parse each row of the data.frame individually
However, in your question, you ask for the "first occurrence of formatted_address
under location".
In the row of data you've given, there are seven different locations, which you can see by the number of place_id
values, and there is only one formatted_address
per place_id
.
So asking for the first occurrence doesn't make sense, as there is only one.
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
As per the comments - to access just the first entry per each JSON object, you can just subset the first element in the 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
回答2:
Here is one possibility:
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)
I hope this is what you need.
来源:https://stackoverflow.com/questions/44307012/unable-to-access-data-from-json-object