简体   繁体   中英

JSON: Is it possible to have a key with more than one value?

<----Disclaimer----> I like to give lots of details just to be safe, feel free to skip to the problem if you don't need the backstory.

<-----Backstory---->

I am developing a touchscreen directory that will display all of the employees at my center. The directory is broken up into departments (Management, Sales, Standards, etc) When we started building the JSON files for the directory we broke them up by department so we had a management.json file, sales.json and so on. We are about to finish version 1.0 for the directory and deploy it however, now that we have built all of the JSON files we see there is a slight problem. Many employees fit into more than one category, this wasn't a huge issue when we had all of the JSON files seperated, because we could have and entry for "Mark Jones" in the sales.json and management.json and there would be no conflict. Someone suggested however, and correctly so, that it would be great to be able to filter employees based on certain criteria (best of the best instructors, certified evaluators, etc). I cannot see how to do it without having only one JSON file.

<----Problem---->

We would like to put all of our employees in one JSON file and be able to filter through it using AngularJS. So that brings me to my question, can I have two values (or more I suppose) attached to one key? As follows:

<----Current structure----->
(management.json)

[{
    "fName": "Tom",
    "lName": "Jones",
    "bobAward": "false",
    "department": "management"
}, {
    "fName": "Bill",
    "lName": "Hunter",
    "bobAward": "true",
    "department": "management"
}]

(sales.json)

[{
    "fName": "Tom",
    "lName": "Jones",
    "bobAward": "false",
    "department": "sales"
}, {
    "fName": "Steve",
    "lName": "Webb",
    "bobAward": "true",
    "department": "sales"
}]

<----Proposed file---->
(employees.json)

[{
    "fName": "Tom",
    "lName": "Jones",
    "bobAward": "false",
    "department": "management sales"
}, {
    "fName": "Bill",
    "lName": "Hunter",
    "bobAward": "true",
    "department": "management"
}, {
    "fName": "Steve",
    "lName": "Webb",
    "bobAward": "true",
    "department": "sales"
}]

I would like to be able to filter for department == sales and have Tom and Steve come up, and filter for department == management and have Tom and Bill come up. Let me know if you need anymore information or if there is a simpler way to do this and I am completely lost :)

Note: We do not have access to any servers so any server reliant languages are off the table for us.

Whatever you do, each employee needs a unique integer ID. Otherwise you'll sooner or later hire a second John Smith and learn the hard way about unique identifiers. If your DB people are making a real obstacle of themselves on this point (i infer some organizational dysfunction from your remark about access to servers), I'm not sure what to recommend. But Option 1 below might be better in that case.

Option #1:

"department": ["sales", "management"],

Option #2:

Put departments in a second "table" (figuratively speaking); this is database 101. Use your employee ID numbers to specify membership. If there's simply no way to establish proper unique identifiers for employees, this is off the table.

"departments": [
    {
        "name": "sales",
        "members": [ 2, 12, 13 ]
    },
]

I refined my answer for what you actually need:
http://www.jsoneditoronline.org/?id=24b116863b78d086722b52be21a83221

This represents the JSON format, like:

[{
  "fName": "Tom",
  "lName": "Jones",
  "departments": {
    "sales": null,
    "management": {"joined-date":"2014-06-01T00:00:00", "position":"Senior Manager"},
    "management sales": null
  }
}]

So basically, all Object Nodes are added by the curly brackets { "x":"y" } and Arrays as in Square brackets [ "x","y" ], all recursively.

Then validate your JSON here: jsonformatter .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM