Representing logic as data in JSON

If you must implement this using standard JSON, i’d recommend something akin to Lisp’s “S-expressions”. A condition could be either a plain object, or an array whose first entry is the logical operation that joins them.

For example:

["AND",
    {"var1" : "value1"},
    ["OR",
        { "var2" : "value2" },
        { "var3" : "value3" }
    ]
]

would represent var1 == value1 AND (var2 == value2 OR var3 == value3).

If you prefer brevity over consistency, you could also allow an object to have multiple properties, which would implicitly be joined by an AND. For example, { "a": "b", "c": "d" } would be equivalent to ["AND", { "a": "b" }, { "c": "d" }]. But there are cases (like the example) where the former syntax can not faithfully represent the condition as written; you’d need additional trickery like translating the condition or using dummy property names. The latter syntax should always work.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)