0

I'm running a POST to a 3rd party API in R and the data don't look properly formatted. I'm trying to figure out how to convert the results to a data frame. I'm wondering if maybe I'm not using the correct POST syntax? Sorry if this is obvious, I'm new to JSON..

My call in R is:

res <- httr::POST(url,  body = jSend, add_headers ("Content-Type" = "application/json", "apiKey" = apikey)) 

And the result is supposed to be JSON and look like this:

"valid": true, 
"msg": "", 
"result": { 
    "CALL_INDICATOR_AP_AR": "AP", 
    "TAXPAYER_NUMBER": "xxxxx", 
    "TAXPAYER_NAME": "xxxxx", 
    "TAXPAYER_COUNTRY": "xx", 
    "TAXPAYER_VAT_ID": "xxxxx",

But instead, my result looks like this... Am I calling it incorrectly?

$VALID
[1] TRUE

$MSG
[1] ""

$RESULT
$RESULT$HEADER
$RESULT$HEADER$CALL_INDICATOR_AP_AR
[1] "AR"

$RESULT$HEADER$STORE_FOR_AUDIT
[1] FALSE

$RESULT$HEADER$HOST_NAME
NULL

... 50 variables excluded ...

$RESULT$HEADER$WHT_TREATY_RELIEF
[1] FALSE

$RESULT$HEADER$WHT_PARTICIP_PERCENTAGE
NULL


$RESULT$LINES
$RESULT$LINES[[1]]
$RESULT$LINES[[1]]$FIELDS
$RESULT$LINES[[1]]$FIELDS$INVOICE_LINE_NUMBER
[1] 1

$RESULT$LINES[[1]]$FIELDS$INVOICE_LINE_TYPE
NULL

$RESULT$LINES[[1]]$FIELDS$INVOICE_LINE_DESCRIPTION
NULL

$RESULT$LINES[[1]]$FIELDS$SHIP_FROM_COUNTRY
[1] "NL"

... 50 variables excluded ...

$RESULT$LINES[[1]]$FIELDS$VALITAX_WHT_RATE
[1] "NOLICENSE"

$RESULT$LINES[[1]]$FIELDS$WHT_CATEGORY
NULL

$RESULT$LINES[[1]]$FIELDS$TAX
$RESULT$LINES[[1]]$FIELDS$TAX$JURISDICTION_LEVEL_0
$RESULT$LINES[[1]]$FIELDS$TAX$JURISDICTION_LEVEL_0$RATE_TYPE
[1] "STANDARD_RATE"

$RESULT$LINES[[1]]$FIELDS$TAX$JURISDICTION_LEVEL_0$VALITAX_JURISDICTION_LEVEL
[1] "COUNTRY"

$RESULT$LINES[[1]]$FIELDS$TAX$JURISDICTION_LEVEL_0$VALITAX_JUR_TAX_CODE
[1] ""

$RESULT$LINES[[1]]$FIELDS$TAX$JURISDICTION_LEVEL_0$REP_JUR_TAX_CODE
[1] "I_G_*_DOM_TAXABLE_21_100"




$RESULT$LINES[[1]]$ENGINE_NOTES
list()

$RESULT$LINES[[1]]$EXECUTION_TRAIL
$RESULT$LINES[[1]]$EXECUTION_TRAIL$UserRulesL10Step
$RESULT$LINES[[1]]$EXECUTION_TRAIL$UserRulesL10Step[[1]]
named list()",
Jarvis
  • 31
  • 5
  • You should review the documentation for the API you are accessing. This is certainly not a "standard" format that I recognize. Most API return whatever jested JSON is appropriate for their data. httr automatically parses returned JSON data into a list in R. If you'd rather see the "raw" JSON string, try `content(res, as="text")`. But if you want to work with the data in R you'll need to parse it eventually. – MrFlick Sep 16 '21 at 01:15
  • 1
    The data you've shown is nested and not "rectangular." It's not clear how to turn that into a data.frame automatically. It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Sep 16 '21 at 01:16
  • Thanks @MrFlick, using res <- content(res, as="text") helped. Looking at that, I realized much of it was in a third level list called fields. Called attach(res$RESULT$LINES$FIELDS) and then was able to refer to any of those variables by name. Now I just need to figure out how to create a dataframe with it and add a row to that each time I call the API. Thanks for your help! – Jarvis Sep 17 '21 at 15:17

0 Answers0