Convert JSON keys to lower case and remove special characters in R2019 Community Moderator ElectionRename list of lists using a named listConvert JS object to JSON stringHow to parse JSON to object with lower case keyConvert in R output of package Elastic (nested list?) to data.frame or JSONConvert data.frame into json in RUsing jsonlite to write an unnamed list from R into a json arrayKeep duplicated names in json object from jsonlite conversion of listHow can I encode an R vector of length 1 as a single value in json using the jsonlite R package?Addition of special characters in text string in RXML convert to JSON RParse Nested Json in Python to Remove Special Characters in Columns

A three room house but a three headED dog

Who deserves to be first and second author? PhD student who collected data, research associate who wrote the paper or supervisor?

Placing subfig vertically

Why does the negative sign arise in this thermodynamic relation?

Why is Beresheet doing a only a one-way trip?

Best approach to update all entries in a list that is paginated?

How much stiffer are 23c tires over 28c?

Built-In Shelves/Bookcases - IKEA vs Built

How do I express some one as a black person?

Is Gradient Descent central to every optimizer?

If the Captain's screens are out, does he switch seats with the co-pilot?

Why the color red for the Republican Party

How to create a hard link to an inode (ext4)?

Unreachable code, but reachable with exception

Do items de-spawn in Diablo?

Virginia employer terminated employee and wants signing bonus returned

Making a sword in the stone, in a medieval world without magic

The bar has been raised

What wound would be of little consequence to a biped but terrible for a quadruped?

Space in array system equations

Is it true that real estate prices mainly go up?

In the late 1940’s to early 1950’s what technology was available that could melt a LOT of ice?

What to do when during a meeting client people start to fight (even physically) with each others?

Can someone explain what is being said here in color publishing in the American Mathematical Monthly?



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



2019 Community Moderator ElectionRename list of lists using a named listConvert JS object to JSON stringHow to parse JSON to object with lower case keyConvert in R output of package Elastic (nested list?) to data.frame or JSONConvert data.frame into json in RUsing jsonlite to write an unnamed list from R into a json arrayKeep duplicated names in json object from jsonlite conversion of listHow can I encode an R vector of length 1 as a single value in json using the jsonlite R package?Addition of special characters in text string in RXML convert to JSON RParse Nested Json in Python to Remove Special Characters in Columns










1















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?










share|improve this question






















  • Possible duplicate of Rename list of lists using a named list

    – avid_useR
    Mar 6 at 16:34















1















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?










share|improve this question






















  • Possible duplicate of Rename list of lists using a named list

    – avid_useR
    Mar 6 at 16:34













1












1








1








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?










share|improve this question














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?







r json






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 6 at 16:09









MyQMyQ

1096




1096












  • Possible duplicate of Rename list of lists using a named list

    – avid_useR
    Mar 6 at 16:34

















  • Possible duplicate of Rename list of lists using a named list

    – avid_useR
    Mar 6 at 16:34
















Possible duplicate of Rename list of lists using a named list

– avid_useR
Mar 6 at 16:34





Possible duplicate of Rename list of lists using a named list

– avid_useR
Mar 6 at 16:34












1 Answer
1






active

oldest

votes


















1














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





share|improve this answer

























  • Thanks@Soren but the function does not work properly. Just test this data: x <- list(a = 1, B = 2, C = list( D = 4, e = 5, 'F g' = 'NAME', H = list(i = list(j = list( K = 1, l = list(m = data.frame( a = 1:10, b = runif(10), "c d f g" = runif(10), check.rows = FALSE, check.names = FALSE )) ))) ))

    – MyQ
    Mar 6 at 16:42











  • @MyQ I ran your new list -- first glance seems it did what was needed, although it returned all objects as a list rather than element 'm' as a formatted data frame. Formatting aside, it's all there. See for example "as.data.frame(x1$c$h$i$j$l$m)" which returns the object class and formatting. Is this what you mean by not working properly? What is your expectation in converting back and forth to JSON in terms of retaining object class information?

    – Soren
    Mar 6 at 16:49











  • Thanks Soren. yes the issue is the that your code would manipulate the entire names in the data.frames that could be quite tricky to fix that

    – MyQ
    Mar 6 at 16:50











  • Updated to cast as.data.frame when input object is a data frame, seems to resolve the issue?

    – Soren
    Mar 6 at 16:52










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55027501%2fconvert-json-keys-to-lower-case-and-remove-special-characters-in-r%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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





share|improve this answer

























  • Thanks@Soren but the function does not work properly. Just test this data: x <- list(a = 1, B = 2, C = list( D = 4, e = 5, 'F g' = 'NAME', H = list(i = list(j = list( K = 1, l = list(m = data.frame( a = 1:10, b = runif(10), "c d f g" = runif(10), check.rows = FALSE, check.names = FALSE )) ))) ))

    – MyQ
    Mar 6 at 16:42











  • @MyQ I ran your new list -- first glance seems it did what was needed, although it returned all objects as a list rather than element 'm' as a formatted data frame. Formatting aside, it's all there. See for example "as.data.frame(x1$c$h$i$j$l$m)" which returns the object class and formatting. Is this what you mean by not working properly? What is your expectation in converting back and forth to JSON in terms of retaining object class information?

    – Soren
    Mar 6 at 16:49











  • Thanks Soren. yes the issue is the that your code would manipulate the entire names in the data.frames that could be quite tricky to fix that

    – MyQ
    Mar 6 at 16:50











  • Updated to cast as.data.frame when input object is a data frame, seems to resolve the issue?

    – Soren
    Mar 6 at 16:52















1














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





share|improve this answer

























  • Thanks@Soren but the function does not work properly. Just test this data: x <- list(a = 1, B = 2, C = list( D = 4, e = 5, 'F g' = 'NAME', H = list(i = list(j = list( K = 1, l = list(m = data.frame( a = 1:10, b = runif(10), "c d f g" = runif(10), check.rows = FALSE, check.names = FALSE )) ))) ))

    – MyQ
    Mar 6 at 16:42











  • @MyQ I ran your new list -- first glance seems it did what was needed, although it returned all objects as a list rather than element 'm' as a formatted data frame. Formatting aside, it's all there. See for example "as.data.frame(x1$c$h$i$j$l$m)" which returns the object class and formatting. Is this what you mean by not working properly? What is your expectation in converting back and forth to JSON in terms of retaining object class information?

    – Soren
    Mar 6 at 16:49











  • Thanks Soren. yes the issue is the that your code would manipulate the entire names in the data.frames that could be quite tricky to fix that

    – MyQ
    Mar 6 at 16:50











  • Updated to cast as.data.frame when input object is a data frame, seems to resolve the issue?

    – Soren
    Mar 6 at 16:52













1












1








1







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





share|improve this answer















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






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 6 at 16:52

























answered Mar 6 at 16:31









SorenSoren

1,029511




1,029511












  • Thanks@Soren but the function does not work properly. Just test this data: x <- list(a = 1, B = 2, C = list( D = 4, e = 5, 'F g' = 'NAME', H = list(i = list(j = list( K = 1, l = list(m = data.frame( a = 1:10, b = runif(10), "c d f g" = runif(10), check.rows = FALSE, check.names = FALSE )) ))) ))

    – MyQ
    Mar 6 at 16:42











  • @MyQ I ran your new list -- first glance seems it did what was needed, although it returned all objects as a list rather than element 'm' as a formatted data frame. Formatting aside, it's all there. See for example "as.data.frame(x1$c$h$i$j$l$m)" which returns the object class and formatting. Is this what you mean by not working properly? What is your expectation in converting back and forth to JSON in terms of retaining object class information?

    – Soren
    Mar 6 at 16:49











  • Thanks Soren. yes the issue is the that your code would manipulate the entire names in the data.frames that could be quite tricky to fix that

    – MyQ
    Mar 6 at 16:50











  • Updated to cast as.data.frame when input object is a data frame, seems to resolve the issue?

    – Soren
    Mar 6 at 16:52

















  • Thanks@Soren but the function does not work properly. Just test this data: x <- list(a = 1, B = 2, C = list( D = 4, e = 5, 'F g' = 'NAME', H = list(i = list(j = list( K = 1, l = list(m = data.frame( a = 1:10, b = runif(10), "c d f g" = runif(10), check.rows = FALSE, check.names = FALSE )) ))) ))

    – MyQ
    Mar 6 at 16:42











  • @MyQ I ran your new list -- first glance seems it did what was needed, although it returned all objects as a list rather than element 'm' as a formatted data frame. Formatting aside, it's all there. See for example "as.data.frame(x1$c$h$i$j$l$m)" which returns the object class and formatting. Is this what you mean by not working properly? What is your expectation in converting back and forth to JSON in terms of retaining object class information?

    – Soren
    Mar 6 at 16:49











  • Thanks Soren. yes the issue is the that your code would manipulate the entire names in the data.frames that could be quite tricky to fix that

    – MyQ
    Mar 6 at 16:50











  • Updated to cast as.data.frame when input object is a data frame, seems to resolve the issue?

    – Soren
    Mar 6 at 16:52
















Thanks@Soren but the function does not work properly. Just test this data: x <- list(a = 1, B = 2, C = list( D = 4, e = 5, 'F g' = 'NAME', H = list(i = list(j = list( K = 1, l = list(m = data.frame( a = 1:10, b = runif(10), "c d f g" = runif(10), check.rows = FALSE, check.names = FALSE )) ))) ))

– MyQ
Mar 6 at 16:42





Thanks@Soren but the function does not work properly. Just test this data: x <- list(a = 1, B = 2, C = list( D = 4, e = 5, 'F g' = 'NAME', H = list(i = list(j = list( K = 1, l = list(m = data.frame( a = 1:10, b = runif(10), "c d f g" = runif(10), check.rows = FALSE, check.names = FALSE )) ))) ))

– MyQ
Mar 6 at 16:42













@MyQ I ran your new list -- first glance seems it did what was needed, although it returned all objects as a list rather than element 'm' as a formatted data frame. Formatting aside, it's all there. See for example "as.data.frame(x1$c$h$i$j$l$m)" which returns the object class and formatting. Is this what you mean by not working properly? What is your expectation in converting back and forth to JSON in terms of retaining object class information?

– Soren
Mar 6 at 16:49





@MyQ I ran your new list -- first glance seems it did what was needed, although it returned all objects as a list rather than element 'm' as a formatted data frame. Formatting aside, it's all there. See for example "as.data.frame(x1$c$h$i$j$l$m)" which returns the object class and formatting. Is this what you mean by not working properly? What is your expectation in converting back and forth to JSON in terms of retaining object class information?

– Soren
Mar 6 at 16:49













Thanks Soren. yes the issue is the that your code would manipulate the entire names in the data.frames that could be quite tricky to fix that

– MyQ
Mar 6 at 16:50





Thanks Soren. yes the issue is the that your code would manipulate the entire names in the data.frames that could be quite tricky to fix that

– MyQ
Mar 6 at 16:50













Updated to cast as.data.frame when input object is a data frame, seems to resolve the issue?

– Soren
Mar 6 at 16:52





Updated to cast as.data.frame when input object is a data frame, seems to resolve the issue?

– Soren
Mar 6 at 16:52



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55027501%2fconvert-json-keys-to-lower-case-and-remove-special-characters-in-r%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved