diesel::pg::expression::dsl

Function jsonb_strip_nulls

source
pub fn jsonb_strip_nulls<E: JsonbOrNullableJsonb + SingleValue, jsonb>(
    jsonb: jsonb,
) -> jsonb_strip_nulls<E, jsonb>
where jsonb: 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(jsonb_strip_nulls::<Jsonb, _>(json!({"hello": null})))
    .get_result::<Value>(connection)?;
let expected: Value = json!({});
assert_eq!(result, expected);

let result = diesel::select(jsonb_strip_nulls::<Jsonb, _>(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(jsonb_strip_nulls::<Nullable<Jsonb>, _>(None::<Value>))
    .get_result::<Option<Value>>(connection)?;
assert!(result.is_none());

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