Skip to content

query

q_json_update(field, key, value)

It takes a JSON field, a list of keys, and a value, and returns a RawSQL object that will update the JSON field with the value at the given keys

:param field: The field to update :type field: str :param key: The key to update :type key: List[str] :param value: The value to be inserted into the JSON field :type value: Any :return: A RawSQL object.

Source code in backend/shared/query.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def q_json_update(field: str, key: List[str], value: Any) -> RawSQL:
    """
    It takes a JSON field, a list of keys, and a value, and returns a RawSQL object that will update the JSON field with the
    value at the given keys

    :param field: The field to update
    :type field: str
    :param key: The key to update
    :type key: List[str]
    :param value: The value to be inserted into the JSON field
    :type value: Any
    :return: A RawSQL object.
    """
    key = ','.join(key)

    return RawSQL(f'''
            jsonb_set({field}::jsonb, %s::text[], %s::jsonb, true)
        ''', [
        f'{{{key}}}',
        json.dumps(value),
    ])