Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON data enum types

I have a JSON object like this.

var data={
"Company" : "XYZ",
"company" : ['RX','TX']
}

The above json data has 2 keys Company whose type is string and company whose type is enum but not array(I didnt know how to represent enum in json data),because of which json schema says its an array. I want it to be enum type.

So how will I represent Enum type in JSON data?

like image 384
Akash Sateesh Avatar asked Jun 17 '26 09:06

Akash Sateesh


1 Answers

JSON has no enum type. The two ways of modeling an enum would be:

An array, as you have currently. The array values are the elements, and the element identifiers would be represented by the array indexes of the values. This, however, does not model sparse enums (enums where the first index is not zero OR where the identifiers are not sequential).

enum suit {
  clubs = 0,
  diamonds,
  hearts,
  spades,
};

// is equivalent to

"suitEnum": ["clubs", "diamonds", "hearts", "spades"];

A map, which is less compact but solves the array limitations:

enum suit {
  clubs = 10,
  diamonds = 20,
  hearts = 30,
  spades = 40,
};

// is equivalent to

"suitEnum": {
  "clubs": 10,
  "diamonds": 20,
  "hearts": 30,
  "spades": 40
};
like image 157
Pierre Avatar answered Jun 19 '26 14:06

Pierre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!