diesel::pg::expression::dsl

Function json_strip_nulls

Source
pub fn json_strip_nulls<E: JsonOrNullableJson + SingleValue, json>(
    json: json,
) -> json_strip_nulls<E, json>
where json: AsExpression<E>,
Available on crate feature postgres_backend only.
Expand description

Deletes all object fields that have null values from the given JSON value, recursively.

ยงExample


let result = diesel::select(json_strip_nulls::<Json, _>(json!({"hello": null})))
    .get_result::<Value>(connection)?;
let expected: Value = json!({});
assert_eq!(result, expected);

let result = diesel::select(json_strip_nulls::<Json, _>(json!([{"f1":1, "f2":null}, 2, null, 3])))
    .get_result::<Value>(connection)?;
let expected: Value = json!([{"f1":1}, 2, null, 3]);
assert_eq!(result, expected);

let result = diesel::select(json_strip_nulls::<Nullable<Json>, _>(None::<Value>))
    .get_result::<Option<Value>>(connection)?;
assert!(result.is_none());

let result = diesel::select(json_strip_nulls::<Json, _>(json!(null)))
    .get_result::<Value>(connection)?;
let expected = json!(null);
assert_eq!(result, expected);