{"id":22358391,"url":"https://github.com/thedvlprs/json_parse","last_synced_at":"2025-03-26T13:43:44.524Z","repository":{"id":114654172,"uuid":"273266720","full_name":"thedvlprs/json_parse","owner":"thedvlprs","description":":tada: JavaScript JSON.parse Tutorial","archived":false,"fork":false,"pushed_at":"2020-06-18T14:56:29.000Z","size":806,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-31T15:17:22.375Z","etag":null,"topics":["arrays","javascript","json-parse","reviver-function","stringify","values"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thedvlprs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2020-06-18T14:55:51.000Z","updated_at":"2021-04-18T05:09:57.000Z","dependencies_parsed_at":null,"dependency_job_id":"dbdf4373-322c-4986-be1b-e64a6a9cf80b","html_url":"https://github.com/thedvlprs/json_parse","commit_stats":null,"previous_names":["thedvlprs/json_parse"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thedvlprs%2Fjson_parse","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thedvlprs%2Fjson_parse/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thedvlprs%2Fjson_parse/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thedvlprs%2Fjson_parse/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thedvlprs","download_url":"https://codeload.github.com/thedvlprs/json_parse/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245667554,"owners_count":20652982,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arrays","javascript","json-parse","reviver-function","stringify","values"],"created_at":"2024-12-04T15:14:41.823Z","updated_at":"2025-03-26T13:43:44.494Z","avatar_url":"https://github.com/thedvlprs.png","language":"JavaScript","readme":"# JavaScript JSON.parse Tutorial\n\nJavaScript JSON.parse tutorial shows how to parse JSON strings into JavaScript objects.\n\n \n## JSON\n\n**JSON (JavaScript Object Notation)** is a lightweight data-interchange format. It is easy for humans to read and write and for machines to parse and generate. The official Internet media type for JSON is `application/json`. The JSON filename extension is `.json`.\n\n## JSON.parse\n\nThe `JSON.parse()` method parses a JSON string and creates a 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. The reverse operation is performed with `JSON.stringify()`.\n\n## JSON.parse values\n\nIn the first example, we parse JSON strings into JavaScript values.\n\n`parse_values.js`\n\n```js\nconsole.log(JSON.parse('-3'));\nconsole.log(JSON.parse('12'));\nconsole.log(JSON.parse('true'));\nconsole.log(JSON.parse('\"falcon\"'));\n```\n\nThe example parses and prints `integers`, a `boolean value` and a `string`.\n\n`$ node parse_values.js`\n\n`-3`  \n  \n`12` \n   \n`true`  \n\n`falcon`\n\nThis is the output.\n\n## JSON.parse array\n\nThe next example parses a JSON array string into a JavaScript array.\n\n`parse_array.js`\n\n```js\nlet data = `[\n  {\n    \"id\": 1,\n    \"first_name\": \"Robert\",\n    \"last_name\": \"Schwartz\",\n    \"email\": \"rob23@gmail.com\"\n  },\n  {\n    \"id\": 2,\n    \"first_name\": \"Lucy\",\n    \"last_name\": \"Ballmer\",\n    \"email\": \"lucyb56@gmail.com\"\n  },\n  {\n    \"id\": 3,\n    \"first_name\": \"Anna\",\n    \"last_name\": \"Smith\",\n    \"email\": \"annasmith23@gmail.com\"\n  }\n]`;\n\nlet users = JSON.parse(data);\n\nconsole.log(typeof users)\nconsole.log(users[1])\nconsole.log(users);\n```\n\nWe have a JSON string consisting of users. The string is parsed into a JavaScript array.\n\n`let users = JSON.parse(data);`\n\nThe data is parsed.\n\n`console.log(typeof users)`\n\nWe determine the data type of the returned data.\n\n`console.log(users[1])`\n\nWe print the second user.\n\n`console.log(users);`\n\nWe print the whole array.\n\n`$ node parse_list.js`\n\n```json\nobject\n{\n  id: 2,\n  first_name: 'Lucy',\n  last_name: 'Ballmer',\n  email: 'lucyb56@gmail.com'\n}\n[\n  {\n    id: 1,\n    first_name: 'Robert',\n    last_name: 'Schwartz',\n    email: 'rob23@gmail.com'\n  },\n  {\n    id: 2,\n    first_name: 'Lucy',\n    last_name: 'Ballmer',\n    email: 'lucyb56@gmail.com'\n  },\n  {\n    id: 3,\n    first_name: 'Anna',\n    last_name: 'Smith',\n    email: 'annasmith23@gmail.com'\n  }\n]\n```\n\nThis is the output.\n\n## JSON.parse reviver function\n\nThe `JSON.parse()` function can take an optional reviver function as the second parameter. It can perform a transformation on the resulting object before it is returned.\n\n`reviver.js`\n\n```js\nlet data = '{ \"name\": \"John Doe\", \"dateOfBirth\": \"1976-12-01\", \"occupation\": \"gardener\"}';\n\nlet user = JSON.parse(data, (k, v) =\u003e {\n\n  if (k == \"dateOfBirth\") {\n    return new Date(v);\n  } else {\n    return v;\n  }\n});\n\nconsole.log(user);\n```\n\nIn the example, we use the **reviver function** to transform a string property into a date.\n\n## JSON.stringify\n\nThe `JSON.stringify()` function converts a JavaScript object or value to a JSON string.\n\n`stringify.js`\n\n```js\nlet users = [\n    {\n        id: 1,\n        first_name: 'Robert',\n        last_name: 'Schwartz',\n        email: 'rob23@gmail.com'\n    },\n    {\n        id: 2,\n        first_name: 'Lucy',\n        last_name: 'Ballmer',\n        email: 'lucyb56@gmail.com'\n    },\n    {\n        id: 3,\n        first_name: 'Anna',\n        last_name: 'Smith',\n        email: 'annasmith23@gmail.com'\n    }\n];\n\nlet data = JSON.stringify(users);\n\nconsole.log(typeof data);\nconsole.log(typeof users);\n\nconsole.log(data);\nconsole.log(users);\n```\n\nIn the example, we have an array of users. We transform the array into a JSON string with the `JSON.stringify()` function.\n\n`$ node stringify.js`\n\n```json\nstring\nobject\n[{\"id\":1,\"first_name\":\"Robert\",\"last_name\":\"Schwartz\",\"email\":\"rob23@gmail.com\"},\n  {\"id\":2,\"first_name\":\"Lucy\",\"last_name\":\"Ballmer\",\"email\":\"lucyb56@gmail.com\"},\n  {\"id\":3,\"first_name\":\"Anna\",\"last_name\":\"Smith\",\"email\":\"annasmith23@gmail.com\"}]\n[\n  {\n    id: 1,\n    first_name: 'Robert',\n    last_name: 'Schwartz',\n    email: 'rob23@gmail.com'\n  },\n  {\n    id: 2,\n    first_name: 'Lucy',\n    last_name: 'Ballmer',\n    email: 'lucyb56@gmail.com'\n  },\n  {\n    id: 3,\n    first_name: 'Anna',\n    last_name: 'Smith',\n    email: 'annasmith23@gmail.com'\n  }\n]\n```\n\nThis is the output.\n\n![](demo.png)\n\nIn this tutorial, we have **parsed JSON strings into JavaScript objects with the** `JSON.parse()` **function**.","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthedvlprs%2Fjson_parse","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthedvlprs%2Fjson_parse","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthedvlprs%2Fjson_parse/lists"}