Uppercase Vs Lowercase
I have a simple problem. I have multiple ways to get a JSON from other apps that i dont control. All the keys have the same name but some are uppercase and some are lowercase. How
Solution 1:
You may normalize properties to be all in uppercase or lowercase:
// Customers with a sample customer as provided in some comment by the OPconst customers = [{
"account_id": 1,
"account_type_description": "Single Account",
"customer_id": 1,
"first_name": "Peter",
"last_name": "Parker",
"identity_card_number": 128,
"tax_identification": 35,
"birth_date": "2018-06-28T07:57:23Z",
"customer_gender_description": "Male",
"street_address": "Gotham Street 56",
"postal_code": "21312",
"city": "Gotham",
"country_description": "Portugal"
},
{
"ACCOUNT_ID": 1,
"ACCOUNT_TYPE_DESCRIPTION": "Single Account",
"CUSTOMER_ID": 1,
"FIRST_NAME": "Peter",
"LAST_NAME": "Parker",
"IDENTITY_CARD_NUMBER": 128,
"TAX_IDENTIFICATION": 35,
"BIRTH_DATE": "2018-06-28T07:57:23Z",
"CUSTOMER_GENDER_DESCRIPTION": "Male",
"STREET_ADDRESS": "Gotham Street 56",
"POSTAL_CODE": "21312",
"CITY": "Gotham",
"COUNTRY_DESCRIPTION": "Portugal"
}
]
constnormalizeProperties = entities => entities.map(customer => {
const outputCustomer = {}
for (let property ofObject.keys(customer))
outputCustomer[property.toLowerCase()] = customer[property]
return outputCustomer
})
const normalizedCustomers = normalizeProperties(customers)
console.log(JSON.stringify(normalizedCustomers))
There're many approaches to get the same result, but the idea is that you don't want those differences once you bind the model to the UI.
Solution 2:
I'd go with the properties normalization, i.e with lodash you can use mapKeys
:
const input = { CUSTOMER_ID: 1, name: 'foo' };
const normalized = _.mapKeys(input, (value, key) => key.toLowerCase());
// {customer_id: 1, name: "foo"}console.log(normalized.customer_id); // 1
If the properties may be written as camelCase, you can normalize also those with snakeCase
:
const input = { CUSTOMER_ID: 1, otherId: 3, name: 'foo' };
const normalized = _.mapKeys(input, (value, key) => _.snakeCase(key));
// {customer_id: 1, other_id: 5, name: "foo"}console.log(normalized.other_id); // 5
Solution 3:
To ignore case for all keys in the JSON, you can define a custom getter for the object like below
Object.defineProperty(Object.prototype, "getProp", {
value: function (prop) {
var key,self = this;
for (key in self) {
if (key.toLowerCase() == prop.toLowerCase()) {
returnself[key];
}
}
},
enumerable: false
});
Now you can get properties ignoring the case like this
obj.getProp("CUSTOMER_ID");
//or
obj.getProp("customer_id");
Solution 4:
You could implement your own parsing function and call it on all JSON responses to normalise them.
E.g. Function to normalise all keys to lower case
functiongetParsedObjectWithUpperCaseKeys(jsonObject) {
var parsedObjectWithUpperCaseKeys = {};
Object.keys(jsonObject).forEach(function (jsonObjectKey) {
parsedObjectWithUpperCaseKeys [jsonObjectKey.toUpperCase()] = jsonObject[jsonObjectKey];
});
return parsedObjectWithUpperCaseKeys;
}
And in your controller code:
// Get Customers
promises.customers.$promise.then(function (data) {
var customers = data["Customers"].Customers;
$scope.customers = getParsedObjectWithUpperCaseKeys(customers);
if(!angular.isArray($scope.customers)) {
$scope.customers = [getParsedObjectWithUpperCaseKeys(customers)];
} else {
$scope.customers = [getParsedObjectWithUpperCaseKeys(customers[0])];
}
console.log($scope.customers);
}.bind(this));
Post a Comment for "Uppercase Vs Lowercase"