import json


def json_meta_command(chat_secret, code):
    """
    Determine if code represents a meta instruction for the chat system of the
    form: whitespaces #chat_secret/json-string
    :param chat_secret: secret string identifying session.
    :param code: code sent through the system.
    :return: json representing the meta instruction or None if none;
    session parameter must match session_id in code.
    """
    stripped = code.lstrip()
    instruction = None
    if stripped and stripped[0] == '#':
        session_end = len(chat_secret) + 1
        if stripped[1:session_end] == chat_secret:
            if stripped[session_end] == '/':
                rest = stripped[session_end+1:]
                try:
                    instruction = json.loads(rest)
                except json.JSONDecodeError:
                    pass
                if not isinstance(instruction, dict):
                    instruction = None
                elif 'sender_client_id' not in instruction:
                    instruction = None
                elif 'sender' not in instruction:
                    instruction = None
                elif 'recipient_client_id' not in instruction:
                    instruction = None
                elif 'recipient' not in instruction:
                    instruction = None
                elif 'command' not in instruction:
                    instruction = None
                elif 'parameters' not in instruction:
                    instruction = None
    return instruction