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!

Jonathan Portorreal
  • 2,730
  • 4
  • 21
  • 38
  • 3
    FYI - JSON is a string notation - you're working with javascript objects – Jaromanda X Feb 27 '17 at 00:28
  • 2
    Possible duplicate of [How to get the JSON with duplicate keys completely in javascript](http://stackoverflow.com/questions/30842675/how-to-get-the-json-with-duplicate-keys-completely-in-javascript) – samson Feb 27 '17 at 00:32
  • 2
    Is the "JSON object" ([there's no such thing](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/)) 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 '17 at 00: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. – Victor Lecomte Feb 27 '17 at 00: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 '17 at 00:55
  • 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 – Mehdi Feb 27 '17 at 00:59
  • In Python, see here on how to create a JSON array from duplicates: https://stackoverflow.com/a/61416136/7471760 – ferdymercury Apr 28 '20 at 13:24

2 Answers2

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.

subwaymatch
  • 1,020
  • 10
  • 11
  • 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. – Victor Lecomte Feb 27 '17 at 00: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()). – subwaymatch Feb 27 '17 at 00:57
  • I push all the filters on the jsonArray and so I get the duplicates. See above, for the code. – Victor Lecomte Feb 27 '17 at 02: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)
Chris Chen
  • 1,228
  • 9
  • 14
  • 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? – Victor Lecomte Feb 27 '17 at 02: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 '17 at 02:21