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 Feb 27, 2017 at 0:28
  • 2
    Possible duplicate of How to get the JSON with duplicate keys completely in javascript
    – samson
    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.
    – nnnnnn
    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. Feb 27, 2017 at 0:46
  • @VictorLecomte—I think samson's duplicate is a duplicate. Your options are to fix the JSON or parse it yourself, which is what the "duplicate" advises (and even provides a small parser).
    – RobG
    Feb 27, 2017 at 0:55

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.

3
  • 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. Feb 27, 2017 at 0:51
  • 2
    @VictorLecomte Can you post your code here? I don't understand how you could have duplicate keys in the first place (before JSON.stringify()). Feb 27, 2017 at 0:57
  • I push all the filters on the jsonArray and so I get the duplicates. See above, for the code. Feb 27, 2017 at 2:05
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
  • 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? Feb 27, 2017 at 2:10
  • If you can change your JSON format, then code above will meet your need. Please have a look in that jsfiddle.
    – Chris Chen
    Feb 27, 2017 at 2:21

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.