I have a script that looks something like this (though not exactly because I obviously can't give my API key and what not to make it fully repoducible)
library(tidyverse)
library(dplyr)
library(httr)
library(civis)
library(rjson)
library(jqr)
record <- GET(
'https://api.secure.com/v4/data',
accept_json(),
add_headers(Accept = 'application/json',
Authorization = VanAPI)
)
record
then produces a list that looks something like this:
Response [https://api.secure.com/v4/data]
Date: 2021-08-12 20:54
Status: 200
Content-Type: application/json; charset=utf-8
Size: 873 B
{
"items": [
{
"playerId": 12827,
"name": "Player Tiers",
"type": "Dynamic",
"description": null,
"points": 249,
"areSubgroupsSticky": false,
"status": "Active",
...
What is the best way to convert this to a JSON that I can then apply the jqr
package to?
Right now I'm doing:
test <- content(record)
Which produces this output:
$items
$items[[1]]
$items[[1]]$playerId
[1] 12827
$items[[1]]$name
[1] "Player Tiers"
$items[[1]]$type
[1] "Dynamic"
$items[[1]]$description
NULL
$items[[1]]$points
[1] 249
$items[[1]]$areSubgroupsSticky
[1] FALSE
$items[[1]]$status
[1] "Active"
$items[[1]]$subgroups
NULL
$items[[1]]$markedSubgroup
NULL
$items[[2]]
$items[[2]]$playerId
[1] 15723
$items[[2]]$name
[1] "Team Tiers"
$items[[2]]$type
[1] "Dynamic"
$items[[2]]$description
NULL
$items[[2]]$points
[1] 35
$items[[2]]$areSubgroupsSticky
[1] FALSE
$items[[2]]$status
[1] "Active"
$items[[2]]$subgroups
NULL
$items[[2]]$markedSubgroup
NULL
$items[[3]]
$items[[3]]$playerId
[1] 16620
$items[[3]]$name
[1] "Coaches Tiers"
$items[[3]]$type
[1] "Dynamic"
$items[[3]]$description
NULL
$items[[3]]$points
[1] 12
$items[[3]]$areSubgroupsSticky
[1] FALSE
$items[[3]]$status
[1] "Active"
$items[[3]]$subgroups
NULL
$items[[3]]$markedSubgroup
NULL
$nextPageLink
NULL
$count
[1] 3
But then I try to convert it to a JSON using test2 <- rjson::toJSON(test)
but that produces this:
[1] "{\"items\":[{\"playerId\":12827,\"name\":\"Player Tiers\",\"type\":\"Dynamic\",\"description\":null,\"points\":249,\"areSubgroupsSticky\":false,\"status\":\"Active\",\"subgroups\":null,\"markedSubgroup\":null},{\"playerId\":15723,\"name\":\"Team Tiers\",\"type\":\"Dynamic\",\"description\":null,\"points\":35,\"areSubgroupsSticky\":false,\"status\":\"Active\",\"subgroups\":null,\"markedSubgroup\":null},{\"playerId\":16620,\"name\":\"Coaches Tiers\",\"type\":\"Dynamic\",\"description\":null,\"points\":12,\"areSubgroupsSticky\":false,\"status\":\"Active\",\"subgroups\":null,\"markedSubgroup\":null}],\"nextPageLink\":null,\"count\":3}"
Is there a better way to get an easy, clean JSON output?