user2678324
user2678324

Reputation:

How to convert JSON string values to lowercase in Javascript?

I have some JSON data which contains mixture of string and int values. How can I convert all the string values to lowercase?

For example:

{ id: 0, name: "SAMPLe", forms: { formId: 0, id: 0, text: "Sample Text" }}

Desired output:

{ id: 0, name: "sample", forms: { formId: 0, id: 0, text: "sample text" }}

Upvotes: 2

Views: 22655

Answers (5)

SWIK
SWIK

Reputation: 880

Use json-case-convertor https://www.npmjs.com/package/json-case-convertor

const jcc = require('json-case-convertor');
jcc.sentenceCaseValues(jsonData)

Upvotes: 0

hoogw
hoogw

Reputation: 5525

In my case, I want only the properties convert to lowercase, the value(like password, number...etc) remain the same.

my ajax callback result set is:

result = [{FULL_NAME:xxx}, {}, {}....... {}]

I want it to be:

 result = [{full_name:xxx}, {}, {}....... {}]

Here is my working code:

I use mazillar browser native api fetch() instead of old ajax or jquery $get etc.

//   **********  must use self = this ************** 
                // this reference vue-app.  must pass it to self, then pass into callback function (success call back)
                var self = this;  


                fetch(getUser_url).then(function (response) {
                                return response.json();
                        }).then(function (result) {




                                 //------------------------ properties to lowercase ----------------------
                                 // result is upper case, must convert all properties to lowercase, 
                                 // however, the value like password or number MUST remain no change. 

                                 // result = [{}, {}, {}....... {}]
                                    var result_lower_properties= [];

                                    var arrayLength = result.length;

                                    for (var i = 0; i < arrayLength; i++) {

                                        var obj = result[i];
                                        var obj_lower_properties = {};

                                        for (var prop in obj) {

                                                          //console.log(prop.toLowerCase())
                                                          //console.log(obj[prop])

                                                          obj_lower_properties[prop.toLowerCase()] = obj[prop]
                                        }// for

                                        result_lower_properties.push(obj_lower_properties)

                                    }// for

                                  //----------  ENd -------------- properties to lowercase ----------------------





                                 // must use self.user,  do not use this.user, 
                                 // because here, this's scope is just the function (result).   
                                 // we need this reference to vue-app, 
                                 self.user = result_lower_properties;  // [{}, {}, {}]  




    }); // fetch(){}

Upvotes: 0

ErikOlson186
ErikOlson186

Reputation: 11

You need to traverse the object.

function lowerStrings(obj) {
  for (let attr in obj) {
    if (typeof obj[attr] === 'string') {
      obj[attr] = obj[attr].toLowerCase();
    } else if (typeof obj[attr] === 'object') {
      lowerStrings(obj[attr]);
    }
  }
}

var obj = { 
  id: 0, 
  name: "SAMPLe", 
  forms: { formId: 0, id: 0, text: "Sample Text" } 
};

lowerStrings(obj);

console.log(obj);

Upvotes: 0

guest271314
guest271314

Reputation: 1

You can use JSON.stringify(), JSON.parse(), typeof

var data = {
  id: 0,
  name: "SAMPLe",
  forms: {
    formId: 0,
    id: 0,
    text: "Sample Text"
  }
};

var res = JSON.parse(JSON.stringify(data, function(a, b) {
  return typeof b === "string" ? b.toLowerCase() : b
}));

console.log(res)

Upvotes: 5

omarjmh
omarjmh

Reputation: 13896

You need to recurse through the object:

https://jsbin.com/lugokek/1/edit?js,console

var x = { id: 0, name: "SAMPLe", forms: { formId: 0, id: 0, text: "Sample Text" }};

function lower(obj) {
  for (var prop in obj) {
  if (typeof obj[prop] === 'string') {
    obj[prop] = obj[prop].toLowerCase();
  }
  if (typeof obj[prop] === 'object') {
    lower(obj[prop]);
    }
  }
  return obj;
}


console.log(lower(x));

Upvotes: 1

Related Questions