Check if json is valid

@param string $data
@return bool
328594
public function isValidJson(string $data) : bool {
    json_decode($data);
    
    if (json_last_error() !== JSON_ERROR_NONE) {
      return false;
    }
    
    return true;
  }
692637
private function isValidResult($payload): bool
    {
        if (!is_array($payload)) {
            return false;
        }

        $headerValid = array_key_exists('jsonrpc', $payload) && $payload['jsonrpc'] === '2.0';
        $resultValid = array_key_exists('result', $payload);
        $idValid     = array_key_exists('id', $payload);

        return $headerValid && $resultValid && $idValid;
    }
501391
public function isJson($value)
    {
        if (!is_scalar($value) && !method_exists($value, '__toString')) {
            return false;
        }

        json_decode($value);

        return json_last_error() === JSON_ERROR_NONE;
    }
648857
public function getIsAllKeysValid(array $data)
    {
        $keys = array_keys($data);
        foreach($keys as $key)
        {
            if(!$this->getIsValidKey($key))
            {
                return false;
            }
        }
        return true;
    }
1242604
protected function parseJson($data)
    {
        $result = json_decode(trim($data), false);

        return json_last_error() == JSON_ERROR_NONE ? $result : false;
    }
830840
public static function jsonEncode($data)
    {
        $json = json_encode($data);
        // if json_encode failed
        if ($json === false) {
            self::jsonLastError();
        }
        return $json;
    }
1314461
protected function isDataValid($data)
    {
        $this->parser->setData($data);
        $this->parser->parseData();

        return $this->parser->isValid();
    }
992886
protected final function validateUpdateData(array $data = null) {
		if ($data === null && isset($this->_formData)) {
			$data = $this->_formData;
		}
		if ($this->rules) {
			$newRules = [];
			foreach ($data as $key => $v) {
				if (isset($this->rules[ $key ])) {
					$newRules[ $key ] = $this->rules[ $key ];
				}
			}
			if ($newRules) {
				return $this->validateData($newRules, $data);
			}
		}

		return true;
	}
252237
public function validateCompatible(array $data)
    {
        if(!count($data)) {
            /**
             * Allow empty
             */
            return true;
        }
        $count = 0;
        foreach($data as $k=>$v) {
            foreach(array('name','channel','min','max') as $fld) {
                $$fld = trim($v[$fld]);
            }
            $count++;

            $res = $this->validateUrl($channel) && strlen($channel);
            if(!$res) {
                $this->addError("Invalid or empty channel in compat. #{$count}");
            }

            $res = $this->validatePackageName($name) && strlen($name);
            if(!$res) {
                $this->addError("Invalid or empty name in compat. #{$count}");
            }
            $res1 = $this->validateVersion($min);
            if(!$res1) {
                $this->addError("Invalid or empty minVersion in compat. #{$count}");
            }
            $res2 = $this->validateVersion($max);
            if(!$res2) {
                $this->addError("Invalid or empty maxVersion in compat. #{$count}");
            }
            if($res1 && $res2 && $this->versionLower($max, $min)) {
                $this->addError("Max version is lower than min in compat #{$count}");
            }

        }
        return ! $this->hasErrors();
    }
187161
protected function prettyJson($data)
	{
		$json = json_encode($data, Arr::get($this->config, 'json.encode.options', JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP));

		if (json_last_error() !== JSON_ERROR_NONE)
		{
			return false;
		}

		$tab = "\t";
		$newline = "\n";
		$new_json = "";
		$indent_level = 0;
		$in_string = false;
		$len = strlen($json);

		for ($c = 0; $c < $len; $c++)
		{
			$char = $json[$c];
			switch($char)
			{
				case '{':
				case '[':
					if ( ! $in_string)
					{
						$new_json .= $char.$newline.str_repeat($tab, $indent_level+1);
						$indent_level++;
					}
					else
					{
						$new_json .= $char;
					}
					break;
				case '}':
				case ']':
					if ( ! $in_string)
					{
						$indent_level--;
						$new_json .= $newline.str_repeat($tab, $indent_level).$char;
					}
					else
					{
						$new_json .= $char;
					}
					break;
				case ',':
					if ( ! $in_string)
					{
						$new_json .= ','.$newline.str_repeat($tab, $indent_level);
					}
					else
					{
						$new_json .= $char;
					}
					break;
				case ':':
					if ( ! $in_string)
					{
						$new_json .= ': ';
					}
					else
					{
						$new_json .= $char;
					}
					break;
				case '"':
					if ($c > 0 and $json[$c-1] !== '\\')
					{
						$in_string = ! $in_string;
					}
				default:
					$new_json .= $char;
					break;
			}
		}

		return $new_json;
	}
1369800
public static function is($data) {
		if (self::isArray($data)) {
			return 'array';

		} else if (self::isObject($data)) {
			return 'object';

		} else if (self::isJson($data)) {
			return 'json';

		} else if (self::isSerialized($data)) {
			return 'serialized';

		} else if (self::isXml($data)) {
			return 'xml';
		}

		return 'other';
	}
924696
public function validate($data) {
        if (empty($data)) {
            return true;
        }

        try {
            \core_auth\digital_consent::parse_age_digital_consent_map($data);
        } catch (\moodle_exception $e) {
            return get_string('invalidagedigitalconsent', 'admin', $e->getMessage());
        }

        return true;
    }
250796
private function isValidJsonStructure(string $json): bool
    {
        if ('{' === $json[0] && $json[strlen($json) - 1] !== '}') {
            return false;
        } elseif ('[' === $json[0] && $json[strlen($json) - 1] !== ']') {
            return false;
        } elseif ('{' !== $json[0] && '[' !== $json[0]) {
            return false;
        }

        return true;
    }
278477
public function IsValidVData( $arrJson )
	{
		return ( is_array( $arrJson ) &&
			array_key_exists( 'errorid', $arrJson ) &&
			array_key_exists( 'errordesc', $arrJson ) &&
			array_key_exists( 'vdata', $arrJson ) &&
			is_numeric( $arrJson[ 'errorid' ] ) &&
			is_string( $arrJson[ 'errordesc' ] ) &&
			is_array( $arrJson[ 'vdata' ] ) );
	}
563326
protected function validate(array &$data)
    {
        $messages = [];
        if (isset($data['inserts']) && !empty($data['inserts'])) {
            array_push($messages, ['warning', sprintf('Found %s new files. New, not original files may increase the risk of instability.', count($data['inserts']))]);
        }
        if (isset($data['deletes']) && !empty($data['deletes'])) {
            array_push($messages, ['warning', sprintf('%s files has been deleted. Deleting system files may increase the risk of instability.', count($data['deletes']))]);
        }
        if (isset($data['updates']) && !empty($data['updates'])) {
            array_push($messages, ['warning', sprintf('%s files has been changed. File modifications are unacceptable and can increase the risk of instability.', count($data['updates']))]);
        }
        $data = array_merge($data, ['messages' => $messages]);
        return $data;
    }
499563
public static function isJson (string $string) : bool
    {
        if (is_numeric($string) || $string === 'true' || $string === 'false' || $string === 'null' || empty($string)) :
            return false;
        endif;

        // try to decode string
        json_decode($string);

        // check if error occured
        return json_last_error() === JSON_ERROR_NONE;
    }
21214
public function parse_json( $data = null ) {

		if ( ! empty( $data ) ) {
			$this->input = $data;
		}

		$items = @json_decode( $this->input, true );

		if ( ! is_array( $items ) ) {
			return false;
		}

		// Only export to a basic object if building for a single item.
		if ( ! empty( $this->data['single'] ) ) {
			$data = $items;
		} else {
			$data = array(
				'columns' => array(),
				'items'   => array(),
				'fields'  => array(),
			);

			foreach ( $items as $key => $item ) {
				if ( ! is_array( $item ) ) {
					continue;
				}

				foreach ( $item as $column => $value ) {
					if ( ! in_array( $column, $data['columns'], true ) ) {
						$data['columns'][] = $column;
					}
				}

				$data['items'][ $key ] = $item;
			}
		}

		$this->parsed = $data;

		return $this->parsed;
	}