1
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)

First I convert it to a JSON:

capture_json <- rjson::toJSON(capture)

Then I narrow it to only include .customFields and then I filter it on which customFieldId's I want:

capture_jq <- capture_json %>% jqr::jq('.customFields') %>% jq('.[] | select(.customField.customFieldId | IN(83,84,85))')

Using that JSON data, I'm trying to extract the values listed in the assignedValue field. When I use the jq tool and filter by .[]|.assignedValue it returns the values I want:

"Male"
"Caucasian"
"31"

You can see for yourself in this link.

In my R script, the JSON data is stored in a variable called capture_jq and then I run this script:

assigned_values <- capture_jq %>% jq('.[]|."assignedValue"') 

But when I run that script, all it does is return null values that look like this:

[
    null,
    null,
    null
]

What am I doing wrong? Why is it working fine in the jqplay link I provided above but not using the jqr package in R?

wizkids121
  • 634
  • 1
  • 9
  • 22
  • @akrun please check the new reproducible script – wizkids121 Aug 06 '21 at 20:41
  • I got it, but I think what you are getting in `capture` for those "assignedValue" is `"null"`. It could be related to the GET call – akrun Aug 06 '21 at 20:43
  • But it doesn't say `null` in the dput. You can see for example: `assignedValue = "Caucasian"` Furthermore, if I run say `capture_jq %>% jq('.[]|."customFieldId"') ` it does return values – wizkids121 Aug 06 '21 at 20:51
  • You can look at the values in `capture` `$customFields[[25]]$customField$availableValues NULL` – akrun Aug 06 '21 at 21:04
  • I don't get it. If you check my dput output, you can clearly see that the values aren't NULL. To help you sort out the big JSON blob, just search this page for "**" to show you what I'm talking about – wizkids121 Aug 06 '21 at 21:39
  • You may check =your dput itself it shows `customFieldGroupId = 12L, assignedValue = NULL)` – akrun Aug 06 '21 at 21:42
  • Right but that's not what I'm trying to extract. Those values are irrelevant to what I'm trying to extract. If you run `capture_json <- rjson::toJSON(capture)` followed by `capture_jq <- capture_json %>% jqr::jq('.customFields') %>% jq('.[] | select(.customField.customFieldId | IN(83,84,85))')` you'll see what I'm talking about. I'm trying to get `assignedValue` from from `customFieldId` 83, 84, and, 85 – wizkids121 Aug 06 '21 at 21:50
  • I get `capture_jq %>% jq('."assignedValue"') [ "Male", "Caucasian", "31" ]` – akrun Aug 06 '21 at 21:58
  • Ohhhh it was because I was running `capture_jq %>% jq('.[]|."assignedValue"')`. If I run `capture_jq %>% jq('."assignedValue"')` it works. – wizkids121 Aug 06 '21 at 22:00
  • 1
    Thank you for your help and patience – wizkids121 Aug 06 '21 at 22:01

1 Answers1

1

We can change the syntax to as the structure was slightly different compared to the one OP run it on the site

> capture_jq %>% jq('."assignedValue"')
[
    "Male",
    "Caucasian",
    "31"
]
akrun
  • 874,273
  • 37
  • 540
  • 662