1

I'm trying to generate a JSON string which will contain all of my filters, but I constantly stuck with duplicate keys. So, I want to find a solution that turns the duplicate keys into a JSON array.

For example, I have this JSON object:

{
  "filter-1": "value-1",
  "filter-1": "value-2",
  "filter-2": "value-3",
  "filter-3": "value-4"
}

And I want to turn it into this:

{
  "filter-1": ["value-1", "value-2"],
  "filter-2": "value-3",
  "filter-3": "value-4"
}

Can someone point me in the right direction? I would appreciate solutions in JavaScript but any method would be more than welcome! Thanks in advance!

7
  • 3
    FYI - JSON is a string notation - you're working with javascript objects Commented Feb 27, 2017 at 0:28
  • 2
    Possible duplicate of How to get the JSON with duplicate keys completely in javascript Commented Feb 27, 2017 at 0:32
  • 2
    Is the "JSON object" (there's no such thing) that you show a string? If so, the best solution is to fix whatever generates that JSON string, so please edit your question to show the code that does that. Commented Feb 27, 2017 at 0:32
  • 1
    Sorry for the confusion guys. Junior web developer here, thanks for the info. @samson I read that question, but it's not exactly what I'm looking for. This is why I asked if someone could possibly help me out. Commented Feb 27, 2017 at 0:46
  • 1
    An object cannot have duplicate keys. Even if you create JSON string with duplicate keys, when you will parse it, it will get rid of duplicate key by maintaining the value of the last key. Where do you get that object from? the source of this object needs to create an array where duplicate keys are possible Commented Feb 27, 2017 at 0:59

2 Answers 2

0

The duplicate key-pairs are causing overwrite issues.

Javascript objects doesn't allow duplicate keys.

var testObj = JSON.parse('{"filter-1":"value-1","filter-1":"value-2","filter-2":"value-3","filter-3":"value-4"}'); will overwrite the first key-pair (filter-1: value-1) when it parses the second key-pair (filter1: value-2) since both key-pairs have the same key.

However, JSON specification (not Javscript objects) does not specifically mention whether duplicate keys are allowed or not. You may wish to write your own parsing function to handle the duplicate keys.

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

3 Comments

This is weird, because I used JSON.stringify in order to generate the JSON and it hold the duplicate keys. The problem is I don't have the slightest idea how should I turn the duplicates into an array which would contain the values. I thought of using a map or another data structure, but it seems too messy solution.
@VictorLecomte Can you post your code here? I don't understand how you could have duplicate keys in the first place (before JSON.stringify()).
I push all the filters on the jsonArray and so I get the duplicates. See above, for the code.
0

You will have to change the format of your JSON since keys in JS objects must be unique. Then you can hard coded or use libraries like jquery or underscorejs to group them out.

https://jsfiddle.net/p5fkjcwt/1/

var objects = 
{ 
   0: {"filter": "filter-1", "value":"value-1"},
   1: {"filter": "filter-1", "value":"value-2"},
   2: {"filter": "filter-2", "value":"value-3"},
   3: {"filter": "filter-3", "value":"value-4"}
}

var result = _.groupBy(objects,"filter")

console.log(result)

2 Comments

The issue is that I want to get rid of the duplicates and instead to create arrays with the equivalent values. Sorry for the confusion. Do you happen to have any idea?
If you can change your JSON format, then code above will meet your need. Please have a look in that jsfiddle.

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.