-2

I have a requirement to throw a 400 Bad request error if the json payload contains duplicate keys. I am using below code to fetch all attributes in an array.

var arrayObj = [];
var attrArr = [];
var arr = {
  "serviceNumer": "1612045709",
  "customerRefNumber": "TCreateS9",
  "customerRefNumber": "TCreateS9"
};
for (var key in arr) {
  arrayObj.push(key, arr[key]);
}
console.log("arrayObj", arrayObj);
for (var i = 0; i < arrayObj.length; i = i + 2) {
  attrArr.push(arrayObj[i]);
}
console.log(attrArr);

When I iterate using for..in, the duplicate keys get overridden. So please help me with any alternate approach.

Maddy
  • 11
  • 2
  • there is no issues in your code – Sagar V Aug 24 '17 at 10:01
  • 1
    _“When I iterate using for..in, the duplicate keys get overridden”_ - that is not the for...in loop’s “fault” ... you already lose the key when this JSON gets parsed into an object - `console.log(arr)` would have shown you that. – CBroe Aug 24 '17 at 10:01
  • JSON doesn't directly negate the presence of duplicated keys. But when accessing the property through the `object.key` or `object[key]`, the last value will be returned. – quirimmo Aug 24 '17 at 10:02

4 Answers4

0

your JSON impementation can't handle duplicate keys,

if you take the object you've got from the JSON and convert it back to JSON, and then compare the number of colons in the string against the original. If there are duplicate keys in the original there will be fewer colons in the new JSON.

Such a check is suitable to give warning messages to noobs, but it's not bulletproof. An attacker could use escapes for colons in string values resulting in an increased count. if the requiremnt is critical you'll need to modify the JSON parser to do the check.

Jasen
  • 11,837
  • 2
  • 30
  • 48
0

JavaScript objects cannot have duplicate keys. All the keys must all be unique. Go through the following links, this will clear your doubts StackOverflow JSObj and Finding and solving issues for duplicate keys

nams
  • 81
  • 5
0

A JSON Object can't have duplicate Keys. If you are getting your payload as string than you can do following:

var input = '{"serviceNumer":"1612045709","customerRefNumber":"TCreateS9","customerRefNumber":"TCreateS9"}';

if(input === JSON.stringify(JSON.parse(input)))
   console.log("input has No Duplicate");
else
   console.log("input has Duplicate");

here JSON.parse will convert input to JSON object and will remove duplicate keys

Hope this help you:)

Nemani
  • 778
  • 5
  • 12
  • This may help, except in cases where whitespace (spaces, tabs, newlines) of the input string differs even by 1 character from whitespace as generated by `JSON.stringify()`... – Peter B Aug 24 '17 at 10:14
  • that is correct, but that can be handled by removing all this white spaces from both input and parsed string before comparison. – Nemani Aug 24 '17 at 10:22
  • `JSON.parse()` converts JSON (a string) into a plain Javascript object, not a *JSON object*. There's no such thing as a "JSON object", that's a misnomer. Just FYI. – Lennholm Aug 24 '17 at 10:31
-1

you just dont know, keep do it

//you can hard code it or write it
var arr = {
  "serviceNumer": "1612045709",
  "customerRefNumber": "TCreateS9",
  "customerRefNumber": "TCreateS93333"
};
//but when you call it it just will show the last element
console.log(arr.customerRefNumber)

/*
its like you say 
var ac = 1
var ac = 3
console.log(ac)
it will show 3
*/
//make it different

var arr2 = {
  "serviceNumer": "1612045709",
  "customerRefNumber": "TCreateS9",
  "customerRefNumber2": "TCreateS9"
};
 var a = Object.getOwnPropertyNames(arr).sort()
 var b = Object.getOwnPropertyNames(arr2).sort()
console.log(a)
console.log(b)
KEKUATAN
  • 948
  • 7
  • 21
  • Please give a better explanation than `you just dont know, keep do it` because I don't have a clue what you mean by that. – Peter B Aug 24 '17 at 10:10
  • you cant add same key or that means you cant add same name of object make it different – KEKUATAN Aug 24 '17 at 10:11