Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json multiple keys with same value?

Tags:

json

key

need to store in json different keys with same value, like this:

{
  "key1" : "valueA",
  "key2" : "valueA",
  "key3" : "valueA",
  "key4" : "valueB",
  "key5" : "valueB",
  "key6" : "valueB",
}

But because there will be many keys associated with the same value, is there an option to optimize the code, e.g. using an array for the keys? This was throwing me errors...

{
  ["key1","key2","key3"] : "valueA",
  ["key4","key5","key6"] : "valueB
}
like image 514
user3292965 Avatar asked Dec 03 '14 16:12

user3292965


1 Answers

Nope. In JSON all keys must be strings. The best you could do is:

{
  "key1,key2,key3": "valueA",
  "key4,key5,key6": "valueB"
}

(Or some other delimiter in place of ,.)

But then, of course, you'll need to do some processing after decoding the JSON to split them back up into multiple keys.

However, if your concern is with the cost of sending the data over HTTP, then just make sure your server has gzip compression enabled. It will do a great job of compressing those repeated values.

like image 148
Jordan Running Avatar answered Oct 14 '22 09:10

Jordan Running