How to parse JSON to object with lower case key

javascript, jquery, json

Solution

How about this:

json.replace(/"([^"]+)":/g, 
    function($0, $1) { return ('"' + $1.toLowerCase() + '":'); }
);

The regex captures the key name $1 and converts it to lower case.

Live demo: http://jsfiddle.net/bHz7x/1/

[edit] To address @FabrícioMatté's comment, another demo that only matches word characters: http://jsfiddle.net/bHz7x/4/

Problem

I have some JSON data but all the keys are in UPPER case. How to parse them and convert the keys to lower? I am using jQuery. for example: JSON data: ``` {"ID":1234, "CONTENT":"HELLO"} ``` Desired output: ``` {id:1234, content:"HELLO"} ```

Original source

Related problems