Cara menggunakan json to table javascript

The JSON.parse() static method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.

Show

    JSON.parse(text)
    JSON.parse(text, reviver)
    

    text

    The string to parse as JSON. See the JSON object for a description of JSON syntax.

    reviver Optional

    If a function, this prescribes how each value originally produced by parsing is transformed before being returned. Non-callable values are ignored. The function is called with the following arguments:

    JSON.parse("{}"); // {}
    JSON.parse("true"); // true
    JSON.parse('"foo"'); // "foo"
    JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
    JSON.parse("null"); // null
    
    0

    The key associated with the value.

    JSON.parse("{}"); // {}
    JSON.parse("true"); // true
    JSON.parse('"foo"'); // "foo"
    JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
    JSON.parse("null"); // null
    
    1

    The value produced by parsing.

    The

    JSON.parse("{}"); // {}
    JSON.parse("true"); // true
    JSON.parse('"foo"'); // "foo"
    JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
    JSON.parse("null"); // null
    
    2,
    JSON.parse("{}"); // {}
    JSON.parse("true"); // true
    JSON.parse('"foo"'); // "foo"
    JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
    JSON.parse("null"); // null
    
    3, string, number, boolean, or
    JSON.parse("{}"); // {}
    JSON.parse("true"); // true
    JSON.parse('"foo"'); // "foo"
    JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
    JSON.parse("null"); // null
    
    4 value corresponding to the given JSON text.

    JSON.parse("{}"); // {}
    JSON.parse("true"); // true
    JSON.parse('"foo"'); // "foo"
    JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
    JSON.parse("null"); // null
    
    6

    Thrown if the string to parse is not valid JSON.

    JSON.parse() parses a JSON string according to the , then evaluates the string as if it's a JavaScript expression. The only instance where a piece of JSON text represents a different value from the same JavaScript expression is when dealing with the

    JSON.parse("{}"); // {}
    JSON.parse("true"); // true
    JSON.parse('"foo"'); // "foo"
    JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
    JSON.parse("null"); // null
    
    8 key — see .

    If a reviver is specified, the value computed by parsing is transformed before being returned. Specifically, the computed value and all its properties (in a depth-first fashion, beginning with the most nested properties and proceeding to the original value itself) are individually run through the reviver.

    The reviver is called with the object containing the property being processed as

    JSON.parse(
      '{"p": 5}',
      (key, value) =>
        typeof value === "number"
          ? value * 2 // return value * 2 for numbers
          : value, // return everything else unchanged
    );
    // { p: 10 }
    
    JSON.parse('{"1": 1, "2": 2, "3": {"4": 4, "5": {"6": 6}}}', (key, value) => {
      console.log(key);
      return value;
    });
    // 1
    // 2
    // 4
    // 6
    // 5
    // 3
    // ""
    
    2, and two arguments:
    JSON.parse("{}"); // {}
    JSON.parse("true"); // true
    JSON.parse('"foo"'); // "foo"
    JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
    JSON.parse("null"); // null
    
    0 and
    JSON.parse("{}"); // {}
    JSON.parse("true"); // true
    JSON.parse('"foo"'); // "foo"
    JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
    JSON.parse("null"); // null
    
    1, representing the property name as a string (even for arrays) and the property value. If the reviver function returns
    JSON.parse(
      '{"p": 5}',
      (key, value) =>
        typeof value === "number"
          ? value * 2 // return value * 2 for numbers
          : value, // return everything else unchanged
    );
    // { p: 10 }
    
    JSON.parse('{"1": 1, "2": 2, "3": {"4": 4, "5": {"6": 6}}}', (key, value) => {
      console.log(key);
      return value;
    });
    // 1
    // 2
    // 4
    // 6
    // 5
    // 3
    // ""
    
    6 (or returns no value — for example, if execution falls off the end of the function), the property is deleted from the object. Otherwise, the property is redefined to be the return value. If the reviver only transforms some values and not others, be certain to return all untransformed values as-is — otherwise, they will be deleted from the resulting object.

    Similar to the

    JSON.parse(
      '{"p": 5}',
      (key, value) =>
        typeof value === "number"
          ? value * 2 // return value * 2 for numbers
          : value, // return everything else unchanged
    );
    // { p: 10 }
    
    JSON.parse('{"1": 1, "2": 2, "3": {"4": 4, "5": {"6": 6}}}', (key, value) => {
      console.log(key);
      return value;
    });
    // 1
    // 2
    // 4
    // 6
    // 5
    // 3
    // ""
    
    8 parameter of
    JSON.parse(
      '{"p": 5}',
      (key, value) =>
        typeof value === "number"
          ? value * 2 // return value * 2 for numbers
          : value, // return everything else unchanged
    );
    // { p: 10 }
    
    JSON.parse('{"1": 1, "2": 2, "3": {"4": 4, "5": {"6": 6}}}', (key, value) => {
      console.log(key);
      return value;
    });
    // 1
    // 2
    // 4
    // 6
    // 5
    // 3
    // ""
    
    9, reviver will be last called on the root object with an empty string as the
    JSON.parse("{}"); // {}
    JSON.parse("true"); // true
    JSON.parse('"foo"'); // "foo"
    JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
    JSON.parse("null"); // null
    
    0 and the root object as the
    JSON.parse("{}"); // {}
    JSON.parse("true"); // true
    JSON.parse('"foo"'); // "foo"
    JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
    JSON.parse("null"); // null
    
    1. For JSON text parsing to primitive values, reviver will be called once.

    Note that reviver is run after the value is parsed. So, for example, numbers in JSON text will have already been converted to JavaScript numbers, and may lose precision in the process. To transfer large numbers without loss of precision, serialize them as strings, and revive them to BigInts, or other appropriate arbitrary precision formats.

    JSON.parse("{}"); // {}
    JSON.parse("true"); // true
    JSON.parse('"foo"'); // "foo"
    JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
    JSON.parse("null"); // null
    

    JSON.parse(
      '{"p": 5}',
      (key, value) =>
        typeof value === "number"
          ? value * 2 // return value * 2 for numbers
          : value, // return everything else unchanged
    );
    // { p: 10 }
    
    JSON.parse('{"1": 1, "2": 2, "3": {"4": 4, "5": {"6": 6}}}', (key, value) => {
      console.log(key);
      return value;
    });
    // 1
    // 2
    // 4
    // 6
    // 5
    // 3
    // ""
    

    In order for a value to properly round-trip (that is, it gets deserialized to the same original object), the serialization process must preserve the type information. For example, you can use the

    JSON.parse(
      '{"p": 5}',
      (key, value) =>
        typeof value === "number"
          ? value * 2 // return value * 2 for numbers
          : value, // return everything else unchanged
    );
    // { p: 10 }
    
    JSON.parse('{"1": 1, "2": 2, "3": {"4": 4, "5": {"6": 6}}}', (key, value) => {
      console.log(key);
      return value;
    });
    // 1
    // 2
    // 4
    // 6
    // 5
    // 3
    // ""
    
    8 parameter of
    JSON.parse(
      '{"p": 5}',
      (key, value) =>
        typeof value === "number"
          ? value * 2 // return value * 2 for numbers
          : value, // return everything else unchanged
    );
    // { p: 10 }
    
    JSON.parse('{"1": 1, "2": 2, "3": {"4": 4, "5": {"6": 6}}}', (key, value) => {
      console.log(key);
      return value;
    });
    // 1
    // 2
    // 4
    // 6
    // 5
    // 3
    // ""
    
    9 for this purpose:

    // Maps are normally serialized as objects with no properties.
    // We can use the replacer to specify the entries to be serialized.
    const map = new Map([
      [1, "one"],
      [2, "two"],
      [3, "three"],
    ]);
    
    const jsonText = JSON.stringify(
      map,
      (key, value) => (value instanceof Map ? Array.from(value.entries()) : value),
    );
    
    console.log(jsonText);
    // [[1,"one"],[2,"two"],[3,"three"]]
    
    const map2 = JSON.parse(
      jsonText,
      (key, value) => (key === "" ? new Map(value) : value),
    );
    
    console.log(map2);
    // Map { 1 => "one", 2 => "two", 3 => "three" }
    

    Because JSON has no syntax space for annotating type metadata, in order to revive values that are not plain objects, you have to consider one of the following: