SQLite Forum

Proposed JSON enhancements.
Login
What about a function similar to COALESCE, that *returns the first valid json argument or errors out if none is valid*.

```
JSON_FIRSTVALID(p1,p2,p3,p4,...)
```
With that, one could avoid the somewhat lengthy `IF json_valid(p1) THEN ...`-constructs and make sure a query with `json_extract` (or `->`, `->>`) does not error out but rather returns "something", like

```
JSON_FIRSTVALID(maybe_not_json_column,'{}')->'$.my_field'   --results in NULL if not valid json
```
or

```
JSON_FIRSTVALID(maybe_not_json_column,'{"my_field":"DEFAULT"}')->'$.my_field'  --results in '"DEFAULT"' if not valid json
```

Or just enhance the existing JSON()-function to have 1+ parameters and do exactly that? Makes that pretty compact:

```
JSON(maybe_not_json_column,'{}')->'$.my_field'
```