3

I have below mentioned dataframe in R:

DF

ID        Code
PO-1      123
PO-3      345
PO-4      222

I want to pass the ID in api get specific response from the JSON.

I have the api like "https://test.com/path/code-info?ID=PO-1" with header code(x892edrsre34434f).

JSON Format:

{"test":1,"FormDetails":{"CusName":"Alex",
"City":"NYC","limit":null,"pincode":null,
"refID":"123456",
"channelID":"YUT009"}}

I want to extract the CusName and refID from API output for each ID and rbind it with my DF to get the following output.

ID        Code     CusName      refID
PO-1      123      Alex         YUT009
PO-3      345      Carla        YUT098
PO-4      222      Tim          YUT124

I have tried:

Get_Df<-GET("https://***.com", 
    path  = "path/code-info?Id=",
    query = "PO-1", 
    add_headers(token, API key))
Sophia Wilson
  • 581
  • 3
  • 16

1 Answers1

0

First, your get query does not seems to work correct, try

httr::GET(
  "https://test.com", 
  path  = "path/code-info",
  query = list(Id = "PO-1"),
  add_headers(token, API key)
)

or

httr::GET(
  "https://test.com", 
  path = paste0("path/code-info?Id=", "PO-1"),
  add_headers(token, API key)
)

Then use jsonlite or a different httr::content() type to read your JSON

test_list <- jsonlite::fromJSON(test)
test_list$FormDetails$CusName
test_list$FormDetails$refID

You can then first loop about your dataframe to get all responses and once you have them in e.g. a list, extract all relevant information to a vector. I would use lapply for the first loop and purrr for the extraction. To help you further, I'd need more information on the data.

Thomas
  • 1,252
  • 6
  • 24