0

I am trying to parse JSON that is coming to me in the form of an array of arrays (think a table of data). The issue is that this table may contain arrays or maps as elements and these elements may by empty. Here is an example:

json <- '[[1,"foo",[],{}],[1,"bar",[1],{"foo":"bar"}]]'

# Result is a list of 2 where each sublist is of length 4
jsonlite::fromJSON(json)

# Result is a character vector of length 6
> unname(unlist(jsonlite::fromJSON(json)))
[1] "1"   "foo" "1"   "bar" "1"   "bar"

So when I try and cast this to a 2 by 4 matrix I am getting the wrong answer. I would like [] to map to the string "[]" and {} to "{}" so I don't lose elements. It is totally fine for me to return the nested array as "[1]" instead of parsing it as a list. To me this seems like I need to tell the json parser to stop recursing and treat the elements as characters at a certain point but I can't figure out how to do this. I'm not tied to the jsonlite package so basically anything is fair game as long as it is not slow.

1
  • I didn't think having a nested named list would work into a matrix ({"foo":"bar"}). Frankly, unless all of your objects will be single elements (and you remove either the key or value), it does not make sense to try to map it to a matrix. You may need to consider pruning the whole structure before trying to create a matrix. Commented Jan 22, 2015 at 16:47

1 Answer 1

1

You could recursively iterate the parsed json to find the empty lists and replace them with the values you want. For example

renameEmptyLists <- function(x) {
    if (is.list(x)) {
        if( length(x)==0 ) {
        return(if(!is.null(names(x))) { "{}" } else {"[]"} )
        } else {
            return(lapply(x, renameEmptyLists))
        }
    } else {
        x
    }
}

jj <- jsonlite::fromJSON(json)
unname(unlist(renameEmptyLists(jj)))
# [1] "1"   "foo" "[]"  "{}"  "1"   "bar" "1"   "bar"

And to be clear, you where "loosing" them during the unlist(). If you look at the jj object in my example, you will see that the parse correctly identified the empty list and the empty named list.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.