Function: json-parse-string

json-parse-string is a function defined in json.c.

Signature

(json-parse-string STRING &rest ARGS)

Documentation

Parse the JSON STRING into a Lisp object.

This is essentially the reverse operation of json-serialize, which see. The returned object will be the JSON null value, the JSON false value, t, a number, a string, a vector, a list, a hashtable, an alist, or a plist. Its elements will be further objects of these types. If there are duplicate keys in an object, all but the last one are ignored. If STRING doesn't contain a valid JSON object, this function signals an error of type json-parse-error.

The arguments ARGS are a list of keyword/argument pairs:

The keyword argument :object-type specifies which Lisp type is used to represent objects; it can be hash-table, alist or plist. It defaults to hash-table.

The keyword argument :array-type specifies which Lisp type is used to represent arrays; it can be array (the default) or list.

The keyword argument :null-object specifies which object to use to represent a JSON null value. It defaults to :null.

The keyword argument :false-object specifies which object to use to represent a JSON false value. It defaults to :false.

View in manual

Probably introduced at or before Emacs version 27.1.

Source Code

// Defined in /usr/src/emacs/src/json.c
{
  specpdl_ref count = SPECPDL_INDEX ();

#ifdef WINDOWSNT
  ensure_json_available ();
#endif

  Lisp_Object string = args[0];
  CHECK_STRING (string);
  Lisp_Object encoded = json_encode (string);
  check_string_without_embedded_nulls (encoded);
  struct json_configuration conf =
    {json_object_hashtable, json_array_array, QCnull, QCfalse};
  json_parse_args (nargs - 1, args + 1, &conf, true);

  json_error_t error;
  json_t *object
    = json_loads (SSDATA (encoded), JSON_DECODE_ANY | JSON_ALLOW_NUL, &error);
  if (object == NULL)
    json_parse_error (&error);

  /* Avoid leaking the object in case of further errors.  */
  if (object != NULL)
    record_unwind_protect_ptr (json_release_object, object);

  return unbind_to (count, json_to_lisp (object, &conf));
}