{"id":17035884,"url":"https://github.com/dabeng/json-loop","last_synced_at":"2025-04-12T13:02:14.496Z","repository":{"id":15690112,"uuid":"18427855","full_name":"dabeng/JSON-Loop","owner":"dabeng","description":"JSON Loop is a super easy to use tool class helping you loop the deeply nested JSON object.","archived":false,"fork":false,"pushed_at":"2020-12-02T03:06:19.000Z","size":445,"stargazers_count":19,"open_issues_count":1,"forks_count":6,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-10-31T12:50:26.745Z","etag":null,"topics":["json","json-loop"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dabeng.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-04-04T04:55:58.000Z","updated_at":"2023-01-19T18:34:49.000Z","dependencies_parsed_at":"2022-07-12T15:14:03.144Z","dependency_job_id":null,"html_url":"https://github.com/dabeng/JSON-Loop","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dabeng%2FJSON-Loop","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dabeng%2FJSON-Loop/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dabeng%2FJSON-Loop/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dabeng%2FJSON-Loop/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dabeng","download_url":"https://codeload.github.com/dabeng/JSON-Loop/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223518568,"owners_count":17158689,"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":["json","json-loop"],"created_at":"2024-10-14T08:48:27.785Z","updated_at":"2024-11-07T13:04:48.498Z","avatar_url":"https://github.com/dabeng.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JSON Loop\n![heading](http://dabeng.github.io/JSON-Loop/img/json-loop.svg)\nJSON Loop is a super easy to use tool class helping you loop the deeply nested JSON object. \n\n## **[Demo](http://dabeng.github.io/JSON-Loop/)**\n\n## [Here's the ES6 version](https://github.com/dabeng/json-digger)\n\n## Usage\nNote: Here I don't provide API specification because the following code snippets are demonstractive enough.\n\n### Sample Data\n```javascript\nvar obj = {\n  'id': '1', 'name': 'renyang', 'birth': 1985, 'role': 'manager',\n  'member': [\n    {\n      'id': '2', 'name': 'huangfan', 'birth': 1983, 'role': 'manager',\n      'member': [\n        {'id': '3', 'name': 'chenxiong', 'birth': 1984, 'role': 'engineer'}\n      ]\n    },\n    {\n      'id': '4', 'name': 'yuguang', 'birth': 1981, 'role': 'engineer manager',\n      'member': [\n        {'id': '5', 'name': 'chenjian', 'birth': 1985, 'role': 'engineer'}\n      ]\n    },\n    {\n      'id': '6', 'name': 'deshi', 'birth': 1980, 'role': 'engineer manager',\n      'member': [\n        {'id': '7', 'name': 'haibo', 'birth': 1983, 'role': 'engineer'},\n        {'id': '8', 'name': 'weitao', 'birth': 1987, 'role': 'engineer'},\n        {'id': '9', 'name': 'liuzheng', 'birth': 1986, 'role': 'engineer'},\n        {'id': '10', 'name': 'xiaoxue', 'birth': 1988, 'role': 'engineer'},\n        {'id': '11', 'name': 'xuebin', 'birth': 1982, 'role': 'engineer',\n          'member': [\n            {'id': '12', 'name': 'sam', 'birth': 1980, 'role': 'engineer'},\n            {'id': '13', 'name': 'loklaan', 'birth': 1990, 'role': 'engineer'}\n          ]\n        }\n      ]\n    }\n  ]\n};\n```\t\n\t\n### First of all, create a json loop object with required params\n```javascript\n// the first param is the name of 'Id' property of JSON object and the second one\n// is 'children' property name\nvar jsonloop = new JSONLoop(obj, 'id', 'member');\n```\t\n### Find one node based on unique id\n```javascript\n// node is what we are looking for\njsonloop.findNodeById(obj, '11', function(err, node) {\n  if (err) {\n    console.log(err);\n  } else {\n    console.dir(node);\n  }\n});\n```\n### Find the nodes based on conditions\n```javascript\n// find our uE engineer with her name\njsonloop.findNodes(obj, {'name': 'xiaoxue'}, function(err, nodes) {\n  nodes.forEach(function(node) {\n    console.dir(node);\n  });\n});\n// find young engineers in our team. Here, nodes is an array of node.\njsonloop.findNodes(obj, {'role': /engineer/i, 'birth': {'\u003e=': 1985, '\u003c': 1990}},\n  function(err, nodes) {\n    nodes.forEach(function(node) {\n      console.dir(node);\n    });\n});\n```\n### Find a parent node based on a given node\n```javascript\njsonloop.findNodeById(obj, '11', function(err, node) {\n  if (err) {\n    console.log(err);\n  } else {\n    console.clear();\n    jsonloop.findParent(obj, node, function(err, node) {\n      console.clear();\n      if (err) {\n        console.log(err);\n      } else {\n        console.dir(node);\n      }          \n    });\n  }\n});\n```\n### Find sibling nodes based on a given node\n```javascript\njsonloop.findNodeById(obj, '11', function(err, node) {\n  if (err) {\n    console.log(err);\n  } else {\n    console.clear();\n    jsonloop.findSiblings(obj, node, function(err, siblings) {\n      console.clear();\n      if (err) {\n        console.log(err);\n      } else {\n        siblings.forEach(function(item) {\n          console.dir(item);\n        });\n      }          \n    });\n  }\n});\n```\n### Find ancestor nodes based on a given node\n```javascript\njsonloop.findNodeById(obj, '12', function(err, node) {\n  console.clear();\n  if (err) {\n    console.log(err);\n  } else {\n    jsonloop.findAncestors(obj, node, function(err, ancestors) {\n      console.clear();\n      if (err) {\n        console.log(err);\n      } else {\n        ancestors.forEach(function(item) {\n          console.dir(item);\n        });\n      }          \n    });\n  }\n});\n```\n## Competence\nThe following are posts related to loop through nested json object where JOSN Loop can work best:\n* [\\[Stack Overflow\\] How do I loop through deeply nested properties of a json object?](http://stackoverflow.com/questions/5189387/how-do-i-loop-through-deeply-nested-properties-of-a-json-object)\n* [\\[Stack Overflow\\] json find parent node of node where key named “id” equal to “value”](http://stackoverflow.com/questions/19047906/json-find-parent-node-of-node-where-key-named-id-equal-to-value)\n* [\\[Stack Overflow\\] Find the path of JSON for a key value](http://stackoverflow.com/questions/18758593/find-the-path-of-json-for-a-key-value)\n* [\\[Stack Overflow\\] Constructively manipulating any value/object within a JSON tree of unknown depth](http://stackoverflow.com/questions/3702844/constructively-manipulating-any-value-object-within-a-json-tree-of-unknown-depth)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdabeng%2Fjson-loop","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdabeng%2Fjson-loop","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdabeng%2Fjson-loop/lists"}