8

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
}

1 Answer 1

11

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.

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

1 Comment

I've looked at YAML and XML to see if they could solve the problem, they can't, but do you know if there is any good solution when you need to have multiple keys pointing to the same value (so you can lookup a value by eg. url or by id without having to split up a string etc - I'm looking for a solution which would perform somewhat similar to looking up a property by the key, so it'd scale well above 10000+ entries?

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.