json-editor icon indicating copy to clipboard operation
json-editor copied to clipboard

Feature suggestion: option to include non-required properties in UI but exclude when empty from resulting JSON

Open tupelo-schneck opened this issue 12 years ago • 7 comments

We find the UI for including or excluding non-required properties to be a bit clunky and non-intuitive. We'd prefer to just have all the non-required fields in the UI available to be edited, but have them get excluded from the constructed JSON unless data is entered.

Obviously, we lose the ability to distinguish between "property undefined" and "property value is empty string". We don't mind that.

Here's a bit of code we're using that, in our case, has the desired effect.

var oldObjectEditor = JSONEditor.defaults.editors.object;
JSONEditor.defaults.editors.object = oldObjectEditor.extend({
    isRequired: function (editor) {
        // treat everything as required so that all fields show up in the UI
        return true;
    },
    refreshValue: function () {
        this._super();
        var required = this.schema.required;
        var hasRequired = $.isArray(required);
        for (var i in this.editors) {
            if (!this.editors.hasOwnProperty(i)) continue;
            if (hasRequired && required.indexOf(i) > -1) continue;
            var val = this.value[i];
            if (!val) delete this.value[i];
            else if ($.isEmptyObject(val)) delete this.value[i];
            else if ($.isArray(val) && val.length == 0) delete this.value[i];
        }
    } 
});
JSONEditor.defaults.editors.object.options = oldObjectEditor.options;

We think this might be useful to other users and so propose that it be available as an editor option.

tupelo-schneck avatar Aug 06 '14 16:08 tupelo-schneck

I agree the UI is clunky, but I really don't like losing the ability to distinguish between empty string and undefined. I think there's room for something in between.

I'm not a designer or UX person, but maybe there could be a toggle next to each field to add/remove the property. Removed properties would still be in the form, but grayed out or something. Then for each object, you can specify the default starting condition - which fields are added, which ones are removed and grayed out, and which ones don't appear in the form at all and only in the "add properties" modal.

I can take a pass at making a wire frame for this.

jdorn avatar Aug 11 '14 00:08 jdorn

Thanks. Just to be clear, my suggestion is for an option, only to be used by people who are willing to forgo the distinction between empty and undefined. But I look forward to seeing alternatives in any case.

tupelo-schneck avatar Aug 11 '14 15:08 tupelo-schneck

I second this feature suggestion. From a UX perspective, this is not a good workaround. End-users of these forms will likely be using them for data entry where things like tab stops and form field layout/ordering are critical to effective usage. Having to manually add any non-required fields in this manner, besides not being intuitive, is very inefficient.

Either way, it would be very nice to at least have this behavior documented in the README. It took me a couple hours to figure out that my schemas didn't have an issue, but that non-required properties don't show up in the editor by default and the Properties button is the only way to expose them.

paulcullin avatar Sep 11 '14 22:09 paulcullin

As @pcullin-ratio noticed this is also related to the minItems schema setting which makes JSON Editor behave in a different way depending on the minItems value. When minItems=0 (or it is simply not present in the schema) everything works nice; when minItems > 0 non-required properties are not shown when using tabs to edit array of objects. See #246 for details including reproduction instructions.

mazswojejzony avatar Sep 15 '14 19:09 mazswojejzony

The default behavior is to use the keys of the properties keyword as the starting properties for an object. I introduced a schema keyword defaultProperties to override this behavior.

{
  "type": "object",
  "defaultProperties": ["prop1","prop2"]
}

It sounds like there are 3 different requests here:

  • Improve documentation for defaultProperties (obviously, it's not super clear right now)
  • Make sure the defaultProperties behavior works as intended everywhere (tabbed array minItems bug)
  • Add option to throw out empty non-required values

jdorn avatar Sep 15 '14 20:09 jdorn

I added the option remove_empty_properties. I still need to do a pass over the README, so this option is undocumented for now.

You can either set it on a specific part of the schema:

{
  "type": "object",
  "options": {
    "remove_empty_properties": true
  }
}

or globally during instantiation:

var editor = new JSONEditor(element, {
  schema: {...},
  remove_empty_properties: true
})

jdorn avatar Sep 15 '14 23:09 jdorn

Thank you for this feature, it is very helpful. But I found a couple of corner cases. The option remove_empty_properties is not aware of the schema. It will be nice if it preserves the keys with empty values for the required fields.

For example, if the user enters nothing for following schema, the JSON will be

{
    "header": ""
}

because "header" is required and "innerObject" is not and all of its fields are empty, so it can be treated as an empty field itself. At the same time, if a user changes one field of the inner object, both fields appear

{
    "header": "",
    "innerObject": {
        "field1": "user input",
        "field2": ""
    }
}

which will make the JSON conforming to the schema with minimum of the user interaction

{
    "type": "object",
    "properties": {
        "required": [
            "header"
        ],
        "header": {
            "type": "string"
        },
        "innerObject": {
            "type": "object",
            "required": [
                "field1",
                "field2"
            ],
            "properties": [
                "field1": {
                    "type": "string"
                },
                "field2": {
                    "type": "string"
                }
            ]
        }
    }
}

lykahb avatar Sep 15 '15 17:09 lykahb