#r #jq #jsonlite #rjson #rjsonio
Вопрос:
person <- GET(
"https://api.secure.com/v4/people/111322450?$expand=customFields",
accept_json(),
add_headers(Accept = 'application/json',
Authorization = APIKey)
)
capture <- content(person)
dput(capture)
capture <- list(vanId = 111322450L, firstName = "Joe", lastName = "Smith",
middleName = NULL, suffix = NULL, title = "Mr.", contactMode = "Person",
organizationContactCommonName = NULL, organizationContactOfficialName = NULL,
salutation = "Joe", formalSalutation = "Mr. Smith", additionalSalutation = NULL,
preferredPronoun = NULL, envelopeName = "Joe Smith", formalEnvelopeName = "Mr. Joe Smith",
additionalEnvelopeName = NULL, contactMethodPreferenceCode = NULL,
nickname = NULL, website = NULL, professionalSuffix = NULL,
party = NULL, employer = NULL, occupation = NULL, sex = NULL,
dateOfBirth = NULL, selfReportedRace = NULL, selfReportedEthnicity = NULL,
selfReportedRaces = NULL, selfReportedEthnicities = NULL,
selfReportedGenders = NULL, selfReportedSexualOrientations = NULL,
selfReportedLanguagePreference = NULL, emails = NULL, phones = NULL,
addresses = NULL, recordedAddresses = NULL, identifiers = NULL,
codes = NULL, customFields = list(list(customField = list(
customFieldId = 54L, customFieldParentId = NULL, customFieldName = "If you're in college, which college?",
customFieldGroupId = 12L, customFieldGroupName = "College",
customFieldGroupType = "Contacts", customFieldTypeId = "T",
isEditable = TRUE, maxTextboxCharacters = 75L, availableValues = NULL,
isExportable = TRUE), customFieldId = 54L, customFieldGroupId = 12L,
assignedValue = NULL), list(customField = list(customFieldId = 63L,
customFieldParentId = NULL, customFieldName = "Graduation Year",
customFieldGroupId = 12L, customFieldGroupName = "College",
customFieldGroupType = "Contacts", customFieldTypeId = "S",
isEditable = TRUE, maxTextboxCharacters = NULL, availableValues = list(
list(id = 1L, name = "2017", parentValueId = NULL),
list(id = 2L, name = "2018", parentValueId = NULL),
list(id = 3L, name = "2019", parentValueId = NULL),
list(id = 4L, name = "2020", parentValueId = NULL),
list(id = 5L, name = "2021", parentValueId = NULL),
list(id = 6L, name = "2022", parentValueId = NULL),
list(id = 7L, name = "2023", parentValueId = NULL),
list(id = 8L, name = "2024", parentValueId = NULL),
list(id = 11L, name = "2025", parentValueId = NULL),
list(id = 12L, name = "2026", parentValueId = NULL),
list(id = 9L, name = "Already graduated", parentValueId = NULL),
list(id = 10L, name = "Not applicable", parentValueId = NULL)),
isExportable = TRUE), customFieldId = 63L, customFieldGroupId = 12L,
assignedValue = NULL), list(customField = list(customFieldId = 81L,
customFieldParentId = NULL, customFieldName = "School Organization",
customFieldGroupId = 12L, customFieldGroupName = "College",
customFieldGroupType = "Contacts", customFieldTypeId = "S",
isEditable = TRUE, maxTextboxCharacters = NULL, availableValues = list(
list(id = 1L, name = "Administration/Administrators",
parentValueId = NULL), list(id = 2L, name = "Class Representatives",
parentValueId = NULL), list(id = 3L, name = "Greek Life",
parentValueId = NULL), list(id = 4L, name = "Other",
parentValueId = NULL), list(id = 5L, name = "Student Clubs",
parentValueId = NULL)), isExportable = TRUE),
customFieldId = 81L, customFieldGroupId = 12L, assignedValue = NULL),
list(customField = list(customFieldId = 82L, customFieldParentId = NULL,
customFieldName = "School Organization Name", customFieldGroupId = 12L,
customFieldGroupName = "College", customFieldGroupType = "Contacts",
customFieldTypeId = "T", isEditable = TRUE, maxTextboxCharacters = 75L,
availableValues = NULL, isExportable = TRUE), customFieldId = 82L,
customFieldGroupId = 12L, assignedValue = NULL),
list(customField = list(customFieldId = 83L, customFieldParentId = NULL,
customFieldName = "Gender", customFieldGroupId = 36L,
customFieldGroupName = "Demographics", customFieldGroupType = "Contacts",
customFieldTypeId = "T", isEditable = TRUE, maxTextboxCharacters = 75L,
availableValues = NULL, isExportable = TRUE), customFieldId = 83L,
customFieldGroupId = 36L, assignedValue = "Male"),
list(customField = list(customFieldId = 84L, customFieldParentId = NULL,
customFieldName = "Race", customFieldGroupId = 36L,
customFieldGroupName = "Demographics", customFieldGroupType = "Contacts",
customFieldTypeId = "T", isEditable = TRUE, maxTextboxCharacters = 75L,
availableValues = NULL, isExportable = TRUE), customFieldId = 84L,
customFieldGroupId = 36L, assignedValue = "Caucasian"),
list(customField = list(customFieldId = 85L, customFieldParentId = NULL,
customFieldName = "Age", customFieldGroupId = 36L,
customFieldGroupName = "Demographics", customFieldGroupType = "Contacts",
customFieldTypeId = "T", isEditable = TRUE, maxTextboxCharacters = 75L,
availableValues = NULL, isExportable = TRUE), customFieldId = 85L,
customFieldGroupId = 36L, assignedValue = "31"),
list(customField = list(customFieldId = 70L, customFieldParentId = NULL,
customFieldName = "Are you over 21?", customFieldGroupId = 16L,
customFieldGroupName = "RSVPs", customFieldGroupType = "Contacts",
customFieldTypeId = "S", isEditable = TRUE, maxTextboxCharacters = NULL,
availableValues = list(list(id = 1L, name = "no",
parentValueId = NULL), list(id = 2L, name = "yes",
parentValueId = NULL)), isExportable = TRUE),
customFieldId = 70L, customFieldGroupId = 16L, assignedValue = NULL),
list(customField = list(customFieldId = 12L, customFieldParentId = NULL,
customFieldName = "Opt-in to SMS messages", customFieldGroupId = 9L,
customFieldGroupName = "SMS OPT", customFieldGroupType = "Contacts",
customFieldTypeId = "B", isEditable = TRUE, maxTextboxCharacters = NULL,
availableValues = NULL, isExportable = FALSE), customFieldId = 12L,
customFieldGroupId = 9L, assignedValue = NULL), list(
customField = list(customFieldId = 75L, customFieldParentId = NULL,
customFieldName = "Instagram Handle", customFieldGroupId = 34L,
customFieldGroupName = "Social Media", customFieldGroupType = "Contacts",
customFieldTypeId = "T", isEditable = TRUE, maxTextboxCharacters = 75L,
availableValues = NULL, isExportable = TRUE),
customFieldId = 75L, customFieldGroupId = 34L, assignedValue = NULL),
list(customField = list(customFieldId = 79L, customFieldParentId = NULL,
customFieldName = "Facebook Username", customFieldGroupId = 34L,
customFieldGroupName = "Social Media", customFieldGroupType = "Contacts",
customFieldTypeId = "T", isEditable = TRUE, maxTextboxCharacters = 75L,
availableValues = NULL, isExportable = TRUE), customFieldId = 79L,
customFieldGroupId = 34L, assignedValue = NULL),
list(customField = list(customFieldId = 66L, customFieldParentId = NULL,
customFieldName = "Upload ", customFieldGroupId = 31L,
customFieldGroupName = "Story Collection", customFieldGroupType = "Contacts",
customFieldTypeId = "S", isEditable = TRUE, maxTextboxCharacters = NULL,
availableValues = list(), isExportable = TRUE), customFieldId = 66L,
customFieldGroupId = 31L, assignedValue = NULL),
list(customField = list(customFieldId = 65L, customFieldParentId = NULL,
customFieldName = "Story Collection ", customFieldGroupId = 31L,
customFieldGroupName = "Story Collection", customFieldGroupType = "Contacts",
customFieldTypeId = "B", isEditable = TRUE, maxTextboxCharacters = NULL,
availableValues = NULL, isExportable = TRUE), customFieldId = 65L,
customFieldGroupId = 31L, assignedValue = NULL),
list(customField = list(customFieldId = 64L, customFieldParentId = NULL,
customFieldName = "Story Upload", customFieldGroupId = 31L,
customFieldGroupName = "Story Collection", customFieldGroupType = "Contacts",
customFieldTypeId = "T", isEditable = TRUE, maxTextboxCharacters = 75L,
availableValues = NULL, isExportable = TRUE), customFieldId = 64L,
customFieldGroupId = 31L, assignedValue = NULL),
list(customField = list(customFieldId = 13L, customFieldParentId = NULL,
customFieldName = "I would like to volunteer.", customFieldGroupId = 10L,
customFieldGroupName = "Volunteer", customFieldGroupType = "Contacts",
customFieldTypeId = "B", isEditable = TRUE, maxTextboxCharacters = NULL,
availableValues = NULL, isExportable = FALSE), customFieldId = 13L,
customFieldGroupId = 10L, assignedValue = NULL),
list(customField = list(customFieldId = 38L, customFieldParentId = NULL,
customFieldName = "Text Opt-in (old)", customFieldGroupId = 10L,
customFieldGroupName = "Volunteer", customFieldGroupType = "Contacts",
customFieldTypeId = "B", isEditable = TRUE, maxTextboxCharacters = NULL,
availableValues = NULL, isExportable = TRUE), customFieldId = 38L,
customFieldGroupId = 10L, assignedValue = NULL),
list(customField = list(customFieldId = 62L, customFieldParentId = NULL,
customFieldName = "Are you a high school student?",
customFieldGroupId = 10L, customFieldGroupName = "Volunteer",
customFieldGroupType = "Contacts", customFieldTypeId = "B",
isEditable = TRUE, maxTextboxCharacters = NULL, availableValues = NULL,
isExportable = TRUE), customFieldId = 62L, customFieldGroupId = 10L,
assignedValue = NULL), list(customField = list(customFieldId = 67L,
customFieldParentId = NULL, customFieldName = "Have you done any sort of calling, texting, or door-knocking for a political or issue campaign?",
customFieldGroupId = 10L, customFieldGroupName = "Volunteer",
customFieldGroupType = "Contacts", customFieldTypeId = "S",
isEditable = TRUE, maxTextboxCharacters = NULL, availableValues = list(
list(id = 1L, name = "No", parentValueId = NULL),
list(id = 2L, name = "Yes", parentValueId = NULL)),
isExportable = TRUE), customFieldId = 67L, customFieldGroupId = 10L,
assignedValue = NULL), list(customField = list(customFieldId = 68L,
customFieldParentId = NULL, customFieldName = "Check here if you are a Spanish speaker",
customFieldGroupId = 10L, customFieldGroupName = "Volunteer",
customFieldGroupType = "Contacts", customFieldTypeId = "B",
isEditable = TRUE, maxTextboxCharacters = NULL, availableValues = NULL,
isExportable = TRUE), customFieldId = 68L, customFieldGroupId = 10L,
assignedValue = NULL), list(customField = list(customFieldId = 69L,
customFieldParentId = NULL, customFieldName = "Best way to remind you of your shift?",
customFieldGroupId = 10L, customFieldGroupName = "Volunteer",
customFieldGroupType = "Contacts", customFieldTypeId = "S",
isEditable = TRUE, maxTextboxCharacters = NULL, availableValues = list(
list(id = 1L, name = "Email", parentValueId = NULL),
list(id = 2L, name = "Phone call", parentValueId = NULL),
list(id = 3L, name = "Text", parentValueId = NULL)),
isExportable = TRUE), customFieldId = 69L, customFieldGroupId = 10L,
assignedValue = NULL), list(customField = list(customFieldId = 76L,
customFieldParentId = NULL, customFieldName = "Vote Tripling Contact 1",
customFieldGroupId = 35L, customFieldGroupName = "Vote Tripling",
customFieldGroupType = "Contacts", customFieldTypeId = "T",
isEditable = TRUE, maxTextboxCharacters = 75L, availableValues = NULL,
isExportable = TRUE), customFieldId = 76L, customFieldGroupId = 35L,
assignedValue = NULL), list(customField = list(customFieldId = 77L,
customFieldParentId = NULL, customFieldName = "Vote Tripling Contact 2",
customFieldGroupId = 35L, customFieldGroupName = "Vote Tripling",
customFieldGroupType = "Contacts", customFieldTypeId = "T",
isEditable = TRUE, maxTextboxCharacters = 75L, availableValues = NULL,
isExportable = TRUE), customFieldId = 77L, customFieldGroupId = 35L,
assignedValue = NULL), list(customField = list(customFieldId = 78L,
customFieldParentId = NULL, customFieldName = "Vote Tripling Contact 3",
customFieldGroupId = 35L, customFieldGroupName = "Vote Tripling",
customFieldGroupType = "Contacts", customFieldTypeId = "T",
isEditable = TRUE, maxTextboxCharacters = 75L, availableValues = NULL,
isExportable = TRUE), customFieldId = 78L, customFieldGroupId = 35L,
assignedValue = NULL), list(customField = list(customFieldId = 71L,
customFieldParentId = NULL, customFieldName = "Yes, send me a text on election day to remind me to vote",
customFieldGroupId = 32L, customFieldGroupName = "Voting",
customFieldGroupType = "Contacts", customFieldTypeId = "B",
isEditable = TRUE, maxTextboxCharacters = NULL, availableValues = NULL,
isExportable = TRUE), customFieldId = 71L, customFieldGroupId = 32L,
assignedValue = NULL), list(customField = list(customFieldId = 72L,
customFieldParentId = NULL, customFieldName = "how do you plan on voting?",
customFieldGroupId = 32L, customFieldGroupName = "Voting",
customFieldGroupType = "Contacts", customFieldTypeId = "T",
isEditable = TRUE, maxTextboxCharacters = 75L, availableValues = NULL,
isExportable = TRUE), customFieldId = 72L, customFieldGroupId = 32L,
assignedValue = NULL), list(customField = list(customFieldId = 80L,
customFieldParentId = NULL, customFieldName = "I'm a first-time voter!",
customFieldGroupId = 32L, customFieldGroupName = "Voting",
customFieldGroupType = "Contacts", customFieldTypeId = "B",
isEditable = TRUE, maxTextboxCharacters = NULL, availableValues = NULL,
isExportable = TRUE), customFieldId = 80L, customFieldGroupId = 32L,
assignedValue = NULL)), primaryCustomField = NULL,
contributionSummary = NULL, suppressions = NULL, caseworkCases = NULL,
caseworkIssues = NULL, caseworkStories = NULL, notes = NULL,
scores = NULL, customProperties = NULL, electionRecords = NULL,
membershipStatus = NULL, organizationRoles = NULL, districts = NULL,
surveyQuestionResponses = NULL, finderNumber = NULL, biographyImageUrl = NULL,
primaryContact = NULL)
Сначала я конвертирую его в JSON:
capture_json <- rjson::toJSON(capture)
Затем я сужаю его, чтобы включить только .customFields
, а затем фильтрую по тому, что customFieldId
мне нужно:
capture_jq <- capture_json %>% jqr::jq('.customFields') %>% jq('.[] | select(.customField.customFieldId | IN(83,84,85))')
Используя эти данные JSON, я пытаюсь извлечь значения, перечисленные в assignedValue
поле. Когда я использую инструмент jq и фильтрую по .[]|.assignedValue
нему, он возвращает нужные мне значения:
"Male"
"Caucasian"
"31"
Вы можете сами убедиться в этом по этой ссылке.
В моем сценарии R данные JSON хранятся в переменной с именем capture_jq
, а затем я запускаю этот сценарий:
assigned_values <- capture_jq %>% jq('.[]|."assignedValue"')
Но когда я запускаю этот скрипт, все, что он делает, это возвращает нулевые значения, которые выглядят следующим образом:
[
null,
null,
null
]
Что я делаю не так? Почему он нормально работает по ссылке jqplay, которую я предоставил выше, но не использует jqr
пакет в R?
Комментарии:
1. @akrun, пожалуйста, проверьте новый воспроизводимый сценарий
2. Я понял, но я думаю, что вы получаете
capture
за эти «присвоенные значения»"null"
. Это может быть связано с вызовом GET3. Но в dput об этом не говорится
null
. Вы можете видеть, например:assignedValue = "Caucasian"
Более того, если я запущу, скажемcapture_jq %>% jq('.[]|."customFieldId"')
, что он возвращает значения4.Вы можете посмотреть значения в
capture
$customFields[[25]]$customField$availableValues NULL
5. Спасибо вам за вашу помощь и терпение
Ответ №1:
Мы можем изменить синтаксис, так как структура немного отличалась от той, которую мы запустили на сайте
> capture_jq %>% jq('."assignedValue"')
[
"Male",
"Caucasian",
"31"
]