def update_jstruct(jstruct, elem_struct, val, keep):
    """
        Update json structure (recursive function)

        :param jstruct: jstruct to update
        :param elem_struct: nested field represented as list
        :param val: value of the nested field        
        :param keep: if true write None values instead of skipping them
        :return: json structure updated
    """

    if len(elem_struct) == 1:
        try:      
            if val == '':
                val = None
            if val == None and not keep:
                del jstruct[elem_struct[0]]
            else:
                jstruct[elem_struct[0]] = val

        except:
            print("  [ERR] Can not associate value ", val, "to field", elem_struct[0])
            jstruct[elem_struct[0]] = None
            pass
    else:  
        elem = elem_struct.pop(0)
        jstruct[elem] = update_jstruct(jstruct[elem], elem_struct, val, keep)

    return jstruct