-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.

3
  • there is no issues in your code Commented Aug 24, 2017 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. Commented Aug 24, 2017 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. Commented Aug 24, 2017 at 10:02

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

2 Comments

RFC 7159 section 4 mentions duplicate keys, but does not call them out as syntactically incorrect.
Sorry, a mistake in writing the answer, thanks for pointing out !
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.

Comments

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:)

3 Comments

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()...
that is correct, but that can be handled by removing all this white spaces from both input and parsed string before comparison.
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.
-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)

2 Comments

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.
you cant add same key or that means you cant add same name of object make it different

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.