<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def write_moc_json(moc, filename=None, file=None):
 """Write a MOC in JSON encoding. Either a filename, or an open file object can be specified. """


    moc.normalize()

    obj = {}

    for (order, cells) in moc:
        obj['{0}'.format(order)] = sorted(cells)

    if file is not None:
        _write_json(obj, file)
    else:
        with open(filename, 'wb') as f:
            _write_json(obj, f)