You can use a JSON replacer to switch keys before writing.

JSON.stringify(myVal, function (key, value) {
  if (value && typeof value === 'object') {
    var replacement = {};
    for (var k in value) {
      if (Object.hasOwnProperty.call(value, k)) {
        replacement[k && k.charAt(0).toLowerCase() + k.substring(1)] = value[k];
      }
    }
    return replacement;
  }
  return value;
});

For the opposite, you can use a JSON reviver.

JSON.parse(text, function (key, value) {
    if (value && typeof value === 'object')
      for (var k in value) {
        if (/^[A-Z]/.test(k) && Object.hasOwnProperty.call(value, k)) {
          value[k.charAt(0).toLowerCase() + k.substring(1)] = value[k];
          delete value[k];
        }
      }
      return value;
    });

The second optional argument is a function that is called with every value created as part of the parsing or every value about to be written. These implementations simply iterate over keys and lower-cases the first letter of any that have an upper-case letter.

There is documentation for replacers and revivers at http://json.org/js.html :

The optional reviver parameter is a function that will be called for every key and value at every level of the final result. Each value will be replaced by the result of the reviver function. This can be used to reform generic objects into instances of pseudoclasses, or to transform date strings into Date objects.

The stringifier method can take an optional replacer function. It will be called after the toJSON method (if there is one) on each of the values in the structure. It will be passed each key and value as parameters, and this will be bound to object holding the key. The value returned will be stringified.

Answer from Mike Samuel on Stack Overflow
Top answer
1 of 1
48

You can use a JSON replacer to switch keys before writing.

JSON.stringify(myVal, function (key, value) {
  if (value && typeof value === 'object') {
    var replacement = {};
    for (var k in value) {
      if (Object.hasOwnProperty.call(value, k)) {
        replacement[k && k.charAt(0).toLowerCase() + k.substring(1)] = value[k];
      }
    }
    return replacement;
  }
  return value;
});

For the opposite, you can use a JSON reviver.

JSON.parse(text, function (key, value) {
    if (value && typeof value === 'object')
      for (var k in value) {
        if (/^[A-Z]/.test(k) && Object.hasOwnProperty.call(value, k)) {
          value[k.charAt(0).toLowerCase() + k.substring(1)] = value[k];
          delete value[k];
        }
      }
      return value;
    });

The second optional argument is a function that is called with every value created as part of the parsing or every value about to be written. These implementations simply iterate over keys and lower-cases the first letter of any that have an upper-case letter.

There is documentation for replacers and revivers at http://json.org/js.html :

The optional reviver parameter is a function that will be called for every key and value at every level of the final result. Each value will be replaced by the result of the reviver function. This can be used to reform generic objects into instances of pseudoclasses, or to transform date strings into Date objects.

The stringifier method can take an optional replacer function. It will be called after the toJSON method (if there is one) on each of the values in the structure. It will be passed each key and value as parameters, and this will be bound to object holding the key. The value returned will be stringified.

Discussions

How to serialize properties to lower case using System.Text.Json.JsonSerializer
This browser is no longer supported · Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support More on learn.microsoft.com
🌐 learn.microsoft.com
1
0
October 9, 2021
Slim Build - json.stringify is not a function (lowercase issue)
You want to: report a bug request a feature Current behaviour Uncaught TypeError: json.stringify is not a function Steps to reproduce (if the current behaviour is a bug) Using the new slim build in 1.7 Expected behaviour The code should ... More on github.com
🌐 github.com
0
November 27, 2016
jquery - How to transform all JSON keys to lowercase in javascript? - Stack Overflow
Actually, my requirement is to convert whole JSON(only keys) into lowercase. I tried it's converting only first key of that JSON and it's not converting whole all key. Please have a look the fiddle... More on stackoverflow.com
🌐 stackoverflow.com
July 30, 2016
SerializeJSON with lowercase keys and override JSONConverter.java - language - Lucee Dev
Hi. I am very new to Lucee and loving it! Thanks! I am migrating a CF10 legacy app and all is going well so far. This legacy app basically provides JSON packets for a client side JS app. As such the client app is expecting all json keys to be lower case. The CF10 app uses Ben Nadel’s ... More on dev.lucee.org
🌐 dev.lucee.org
1
July 9, 2022
People also ask

What does JSON stringify do to text?
JSON stringify converts plain text into a JSON-safe string by escaping special characters. Newlines become \n, tabs become \t, double quotes become \", and backslashes become \\. The entire result is wrapped in double quotation marks so it can be safely embedded as a value in a JSON object or array. This ensures that whitespace, line breaks, and other control characters are preserved when the text is stored or transmitted as JSON data.
🌐
convertcase.net
convertcase.net › convert case › json stringify text
JSON Stringify Text | Stringify Text Online | Convert Case
When would I need to stringify text for JSON?
Stringifying text is necessary whenever you need to include multi-line content or text with special characters inside a JSON structure. Common scenarios include preparing API request bodies that contain formatted text, embedding HTML templates or email content within JSON payloads, storing user-generated content in JSON-based databases, and building configuration files where values include line breaks or quotation marks. Without proper stringification, these special characters would break the JSON syntax and cause parsing errors. To reverse the process, use the JSON unstringifier. You can also
🌐
convertcase.net
convertcase.net › convert case › json stringify text
JSON Stringify Text | Stringify Text Online | Convert Case
What is the difference between JSON.stringify() and this tool?
JavaScript's JSON.stringify() method converts a JavaScript value (object, array, string, number) into a JSON string. This tool specifically handles the string escaping part, converting a plain text string into a JSON-safe string literal. If you are working in a browser console, JSON.stringify("your text here") produces the same result.
🌐
convertcase.net
convertcase.net › convert case › json stringify text
JSON Stringify Text | Stringify Text Online | Convert Case
🌐
TutorialsPoint
tutorialspoint.com › how-to-create-a-new-object-from-the-specified-object-where-all-the-keys-are-in-lowercase-in-javascript
How to create a new object from the specified object, where all the keys are in lowercase in JavaScript?
March 6, 2023 - In this example, the JSON.stringify() method is used to convert the original object to a JSON string. The toLowerCase() method is then used to convert all the keys in the JSON string to lowercase.
🌐
Microsoft Learn
learn.microsoft.com › en-us › answers › questions › 584004 › how-to-serialize-properties-to-lower-case-using-sy
How to serialize properties to lower case using System.Text.Json.JsonSerializer - Microsoft Q&A
October 9, 2021 - using System.Text.Json; public async Task<IActionResult> EstoAPICall() { ... EstoOst estoOst; .... var json = JsonSerializer.Serialize(estoOst); StringContent content = new(json, Encoding.UTF8, "application/json"); using var response = await httpClient.PostAsync("https://example.com", content); } public class EstoOst { public decimal Amount { get; set; } } This returns error response because caller requires lower case amount but Serialize method returns upper case Amount. How to fix this ? Switching to Newtosoft or changing class property name to lowercase seems to be not good solutions.
🌐
BigCodeNerd
bigcodenerd.org › blog › converting-json-keys-upper-lower-case-typescript
Converting JSON keys to upper or lower case in Typescript | BigCodeNerd
October 1, 2024 - map() transforms each pair of uppercase/lowercase keys. For nested objects, we recursively call convertCase(). Object.fromEntries() converts the pairs back into an object. ... const originalJson = { name: 'john', age: 30, address: { street: 'main st', city: 'anytown', }, } const converedJSON = convertCase(originalJson) console.log(JSON.stringify(converedJSON, null, 2))
🌐
GitHub
gist.github.com › 4480e8292372da56b426f7a4c65f8774
Convert JSON Keys to lowercase in Javascript · GitHub
Convert JSON Keys to lowercase in Javascript. GitHub Gist: instantly share code, notes, and snippets.
🌐
Online String Tools
onlinestringtools.com › json-stringify-string
JSON Stringify a String – Online String Tools
Quickly convert a string to lowercase. ... Quickly randomize the case of each letter in a string. ... Quickly invert string's case. ... Quickly extract string data from a JSON data structure. ... Quickly convert a string to a JSON string. ... Quickly convert a JSON stringified string to a regular string.
Find elsewhere
🌐
JSONata
docs.jsonata.org › string-functions
String functions · JSONata
Returns a string with all the characters of str converted to lowercase. If str is not specified (i.e. this function is invoked with no arguments), then the context value is used as the value of str.
🌐
Convert Case
convertcase.net › convert case › json stringify text
JSON Stringify Text | Stringify Text Online | Convert Case
Simply enter the text as you would normally on the left panel and see the stringified version generated on the right hand panel, ready to copy and paste. New lines are converted to \n symbols, tabs become \t symbols, and the entire output is wrapped in quotation marks. This is exactly the format required when embedding text values inside JSON data structures.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › JSON › stringify
JSON.stringify() - JavaScript - MDN Web Docs
If it is a number, successive levels in the stringification will each be indented by this many space characters. If it is a string, successive levels will be indented by this string. Each level of indentation will never be longer than 10. Number values of space are clamped to 10, and string values are truncated to 10 characters. ... JSON.stringify({}); // '{}' JSON.stringify(true); // 'true' JSON.stringify("foo"); // '"foo"' JSON.stringify([1, "false", false]); // '[1,"false",false]' JSON.stringify([NaN, null, Infinity]); // '[null,null,null]' JSON.stringify({ x: 5 }); // '{"x":5}' JSON.string
🌐
JSON Formatter
jsonformatter.org › string-utility
Best String Utility
Convert a string to lowercase or uppercase,Character count, Word count, Reverse string, String splitter Detailed character information (decimal, octal, hexadecimal, unicode, html entity, etc.)
🌐
Futurestud.io
futurestud.io › tutorials › how-to-lowercase-keys-of-an-object-in-javascript-or-node-js
How to Lowercase Keys of an Object in JavaScript or Node.js
June 1, 2023 - You can use built-in features of JavaScript to lowercase all keys of an object. Modern browsers and Node.js support the Object.entries() method to return an array of the object’s key-value pairs.
🌐
Lucee Dev
dev.lucee.org › language
SerializeJSON with lowercase keys and override JSONConverter.java - language - Lucee Dev
July 9, 2022 - Hi. I am very new to Lucee and loving it! Thanks! I am migrating a CF10 legacy app and all is going well so far. This legacy app basically provides JSON packets for a client side JS app. As such the client app is expecting all json keys to be lower case. The CF10 app uses Ben Nadel’s JsonSerializer to accomplish that (as well as covering the other ACF serialization issues).
🌐
Experts Exchange
experts-exchange.com › questions › 28663824 › JSON-Keys-to-be-converted-to-lowercase-programmatically.html
Solved: JSON Keys to be converted to lowercase programmatically. | Experts Exchange
April 28, 2015 - In the following angularjs code, $scope.productsNew contains json array whose keys are not in lowercase, they are like Id, Sku, Description I need them to be like id, sku,description I need to change them to lowercase in angularjs, could you help me in this.
🌐
Reddit
reddit.com › r/learnpython › convert json keys to lowercase?
r/learnpython on Reddit: Convert JSON keys to lowercase?
November 3, 2016 -

So this is a pretty strange question, but the default formatting for json is something like this:

{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}

Where the first letter of a new word in a String for the key value is Capitalized.

What I would like to do is get the key values all lowercased so that it looks like this:

{"firstname":"John", "lastname":"Doe"},
{"firstname":"Anna", "lastname":"Smith"},
{"firstname":"Peter", "lastname":"Jones"}

If anyone is wondering why it's because I'm trying to load json data into amazon redshift. For some strange reason loading json values through the auto function that is built into redshift doesn't recognize key values with Uppercase characters since redshift automatically makes the column headers all lowercase. Of course I know this isn't an AWS forum so I will keep that to a minimum, but if anyone knows how I would go about lowercasing the json key values in python that would be great.

🌐
W3Schools
w3schools.com › jsref › jsref_stringify.asp
JavaScript JSON stringify() Method
/*replace the value of "city" to upper case:*/ var obj = { "name":"John", "age":"39", "city":"New York"}; var text = JSON.stringify(obj, function (key, value) { if (key == "city") { return value.toUpperCase(); } else { return value; } }); Try it Yourself »