MyQ
MyQ

Reputation: 469

Convert JSON keys to lower case and remove special characters in R

I am working with jsonlite package in R and want to convert a complex list to JSON object. assume that x is my list:

library(jsonlite)
x= list(a=1,B=2,c=list(D=4,e=5,'F g'='NAME',H=list(i=list(j=list(K=1)))))

x=
$a
[1] 1

$B
[1] 2

$c
$c$D
[1] 4

$c$e
[1] 5

$c$`F g`
[1] "NAME"

$c$H
$c$H$i
$c$H$i$j
$c$H$i$j$K
[1] 1

toJSON(x)
{"a":[1],"B":[2],"c":{"D":[4],"e":[5],"F g":["NAME"],"H":{"i":{"j":{"K":[1]}}}}} 

how can I remove any special case in the JSON keys (like space between F and g as well as lower casing all keys?

I know one option is to operate on the list before feeding into the toJSON() function but even in that case, I have no ideas of how to rename all elements of a list (in particular my list contains some data.frames as well). Is there any regular expression method to do it?

Upvotes: 1

Views: 334

Answers (1)

Soren
Soren

Reputation: 2445

A recursive function to rename all the list of list elements should work. Here, it converts to lower case and also removes all non alpha-numeric characters (spaces, punctuation, etc)

x <- list(a=1,B=2,C=list(D=4,e=5,'F g'='NAME',H=list(i=list(j=list(K=1)))))
renames <- function(x)
{
  cnames <- names(x)
  if (is.null(cnames)) return (x)
  x1 <- lapply(cnames,function(y) renames(x[[y]]))
  if (class(x) %in% "data.frame") x1 <- as.data.frame(x1)
  names(x1) <- gsub("[^[:alnum:]]","",tolower(cnames))
  return(x1)
}

x1 <- renames(x)
> x1
$a
[1] 1

$b
[1] 2

$c
$c$d
[1] 4

$c$e
[1] 5

$c$fg
[1] "NAME"

$c$h
$c$h$i
$c$h$i$j
$c$h$i$j$k
[1] 1

Upvotes: 1

Related Questions