Convert each field in JSON Object from Camel Case to Pascal Case

I have a very complex JSON object with array of objects. I want to convert all Field names from camel case to pascal case. For example :-

Original JSON :-

{ "IsSuccess": true, "ResponseDate": "2019-02-20T11:42:11.963Z", "Result": { "RecordCount": "abc123", "BillDetailsList": [ { "SourceSystem": "abc123", "BillAmount": "abc123", "BillCreationDate": "abc123" }, { "SourceSystem": "abc123", "BillAmount": "abc123", "BillCreationDate": "abc123" } ] } }

New JSON :-

{ "isSuccess": true, "responseDate": "2019-02-20T11:42:11.963Z", "result": { "recordCount": "abc123", "billDetailsList": [ { "sourceSystem": "abc123", "billAmount": "abc123", "billCreationDate": "abc123" }, { "sourceSystem": "abc123", "billAmount": "abc123", "billCreationDate": "abc123" } ] } }

Please help with a Javascript to convert all such JSON Objects.

1 1 11.8K
1 REPLY 1

I've seen this desire before. This is a JavaScript question, not directly related to Apigee Edge.

BTW, the common meaning of these terms:

convention example
camelCase
PascalCase
snake_case

So I think you want to convert from PascalCase to camelCase.

To do this, you need logic to "walk the graph" of the object represented by the JSON, and map the input to a potentially different output. At each node, the logic should look at the property name, and if it is PascalCase, emit an output property with a name in the equivalent camelCase, keeping everything else (the values and data structure) the same. As you can imagine, the "walk the graph" logic needs to be recursive.

I think something like this:

var pascalCasePattern = new RegExp("^([A-Z])([a-z]+)");

function pascalCaseToCamelCase(propname) {
  if (pascalCasePattern.test(propname)) {
    return propname.charAt(0).toLowerCase() + propname.slice(1);
  }
  else {
    return propname;
  }
}

function convertPropertyNames(obj,converterFn) {
  var r, value, t = Object.prototype.toString.apply(obj);
  if (t == "[object Object]") {
    r = {};
    for (var propname in obj) {
      value = obj[propname];
      r[converterFn(propname)] = convertPropertyNames(value, converterFn);
    }
    return r;
  }
  else if (t == "[object Array]") {
    r = [];
    for (var i=0, L=obj.length; i<L; ++i ) {
      value = obj[i];
      r[i] = convertPropertyNames(value, converterFn);
    }
    return r;
  }
  return obj;
}


var originalObject = {
    "IsSuccess": true,
    "ResponseDate": "2019-02-20T11:42:11.963Z",
    "Result": {
        "RecordCount": "abc123",
        "BillDetailsList": [
            {
                "SourceSystem": "Abc123",
                "BillAmount": "Abc123",
                "BillCreationDate": "2019-02-19T09:16:04Z"
            },
            {
                "SourceSystem": "abc123",
                "BillAmount": "XyzAbc",
                "BillCreationDate": "abc123"
            }
        ]
    }
  };

var converted = convertPropertyNames(originalObject, pascalCaseToCamelCase);

This should work without modification in a JS callout. You will need to add things like context.getVariable() and context.setVariable() .