skip to Main Content

A seemingly simple JSON file that I would like to parse with R:

tmp_extract <- "{"encrypted_values":[{"name_a":"value_a"}, {"name_b":"value_b"}, {"name_c":"value_c"}]}"

An attempt with jsonlite::fromJSON produces a dataframe with as many columns as there are names and only a value per column.

tmp_extract |> 
 jsonlite::fromJSON()
$encrypted_values
   name_a  name_b  name_c
1 value_a    <NA>    <NA>
2    <NA> value_b    <NA>
3    <NA>    <NA> value_c

(I also tried tmp_extract |> tidyjson::spread_all() but without success.)

The desired output:

# A tibble: 3 × 2
  name   value  
  <chr>  <chr>  
1 name_a value_a
2 name_b value_b
3 name_c value_c

2

Answers


  1. Try:

    tibble::enframe(unlist(unname(jsonlite::fromJSON(tmp_extract, simplifyDataFrame = FALSE))))
    # # A tibble: 3 × 2
    #   name   value  
    #   <chr>  <chr>  
    # 1 name_a value_a
    # 2 name_b value_b
    # 3 name_c value_c
    
    Login or Signup to reply.
  2. Using the CRAN package rjsoncons and JMESPath query syntax, create in JSON an object with fields name and value

    query <- '{
        "name" : encrypted_values[].keys(@)[],
        "value": encrypted_values[].values(@)[]
    }'
    ## check out our handiwork
    j_query(tmp_extract, query) |> cat("n")
    # {"name":["name_a","name_b","name_c"],"value":["value_a","value_b","value_c"]}
    

    then ‘pivot’ to a tibble (or data.frame)

    j_pivot(tmp_extract, query, as = "tibble")
    

    This approach is performant and flexible; it can be applied to NDJSON too.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search