{"id":16818544,"url":"https://github.com/robb/rbbjson","last_synced_at":"2025-03-17T03:31:52.104Z","repository":{"id":55137848,"uuid":"324174775","full_name":"robb/RBBJSON","owner":"robb","description":"Flexible JSON traversal for rapid prototyping.","archived":false,"fork":false,"pushed_at":"2021-10-13T15:11:31.000Z","size":41,"stargazers_count":162,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-12T16:41:37.609Z","etag":null,"topics":["data-science","json","jsonpath","prototyping","swift"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":false,"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/robb.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"robb"}},"created_at":"2020-12-24T14:32:46.000Z","updated_at":"2025-02-11T10:18:33.000Z","dependencies_parsed_at":"2022-08-14T13:10:22.800Z","dependency_job_id":null,"html_url":"https://github.com/robb/RBBJSON","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/robb%2FRBBJSON","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robb%2FRBBJSON/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robb%2FRBBJSON/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robb%2FRBBJSON/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/robb","download_url":"https://codeload.github.com/robb/RBBJSON/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243841207,"owners_count":20356443,"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":["data-science","json","jsonpath","prototyping","swift"],"created_at":"2024-10-13T10:50:25.167Z","updated_at":"2025-03-17T03:31:51.739Z","avatar_url":"https://github.com/robb.png","language":"Swift","funding_links":["https://github.com/sponsors/robb"],"categories":[],"sub_categories":[],"readme":"# RBBJSON\n\n\u003cp align=\"left\"\u003e\n    \u003cimg src=\"https://img.shields.io/badge/Swift_Version-5.2-orange.svg?style=flat\u0026logo=Swift\" alt=\"Swift Version: 5.2\" /\u003e\n    \u003ca href=\"https://swift.org/package-manager\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/SwiftPM-Compatible-darkgreen.svg?style=flat\" alt=\"Swift Package Manager\" /\u003e\n    \u003c/a\u003e\n      \u003cimg src=\"https://img.shields.io/badge/Platforms-macOS,%20iOS,%20Linux-darkgreen.svg?style=flat\" alt=\"Swift Package Manager\" /\u003e\n    \u003ca href=\"https://twitter.com/DLX\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/Twitter-@DLX-blue.svg?style=flat\u0026logo=Twitter\" alt=\"Twitter: @DLX\" /\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\nRBBJSON enables flexible JSON traversal at runtime and [JSONPath]-like querying for rapid prototyping.\n\nUse `JSONDecoder` to create an `RBBJSON` struct, then traverse it using [dynamic member lookup][dml]:\n\n```swift\nlet json = try JSONDecoder().decode(RBBJSON.self, from: data)\n\njson.firstName         // RBBJSON.string(\"John\")\njson.lastName          // RBBJSON.string(\"Appleseed\")\njson.age               // RBBJSON.number(26)\njson.invalidKey        // RBBJSON.null\njson.phoneNumbers[0]   // RBBJSON.string(\"+14086065775\")\n```\n\nIf you want to access a value that coincides with a Swift-defined property, use a `String` subscript instead:\n\n```swift\njson.office.map     // Error: Maps to Sequence.map\njson.office[\"map\"]  // RBBJSON.string(\"https://maps.apple.com/?q=IL1\")\n```\n\nTo unbox a JSON value, use one of the failable initializers:\n\n```swift\nString(json.firstName) // \"John\"\nString(json.lastName)  // \"Appleseed\"\nString(json.age)       // nil\n\nInt(json.age)          // 26\nDouble(json.age)       // 26.0\n```\n\nYou can also make use of a [JSONPath]-inspired Query syntax to find nested data inside a JSON structure.\n\nFor example, given:\n\n```json\n{ \n  \"store\": {\n    \"book\": [ \n      { \n        \"category\": \"reference\",\n        \"author\": \"Nigel Rees\",\n        \"title\": \"Sayings of the Century\",\n        \"price\": 8.95\n      },\n      { \n        \"category\": \"fiction\",\n        \"author\": \"Evelyn Waugh\",\n        \"title\": \"Sword of Honour\",\n        \"price\": 12.99\n      },\n      { \n        \"category\": \"fiction\",\n        \"author\": \"Herman Melville\",\n        \"title\": \"Moby Dick\",\n        \"isbn\": \"0-553-21311-3\",\n        \"price\": 8.99\n      },\n      { \n        \"category\": \"fiction\",\n        \"author\": \"J. R. R. Tolkien\",\n        \"title\": \"The Lord of the Rings\",\n        \"isbn\": \"0-395-19395-8\",\n        \"price\": 22.99\n      }\n    ],\n    \"bicycle\": {\n      \"color\": \"red\",\n      \"price\": 19.95\n    }\n  }\n}\n```\n\n|JSONPath|RBBJSON|Result|\n|-|-|-|\n|`$.store.book[*].author`|`json.store.book[any: .child].author`|[The authors of all books in the store.](/Tests/RBBJSONTests/READMETests.swift#L46-L51)|\n|`$..author`|`json[any: .descendantOrSelf].author`|[All authors.](/Tests/RBBJSONTests/READMETests.swift#L56-L61)|\n|`$.store.*`|`json.store[any: .child]`|[All things in the store, a list of books an a red bycicle.](/Tests/RBBJSONTests/READMETests.swift#L66-L99)|\n|`$.store..price`|`json.store[any: .descendantOrSelf].price`|[All prices in the store.](/Tests/RBBJSONTests/READMETests.swift#L104-L110)|\n|`$..book[2]`|`json[any: .descendantOrSelf].book[2]`|[The second book.](/Tests/RBBJSONTests/READMETests.swift#L115-L123)|\n|`$..book[-2]`|`json[any: .descendantOrSelf].book[-2]`|[The second-to-last book.](/Tests/RBBJSONTests/READMETests.swift#L128-L136)|\n|`$..book[0,1]`, `$..book[:2]`|`json[any: .descendantOrSelf].book[0, 1])`, `json[any: .descendantOrSelf].book[0...1])`, `json[any: .descendantOrSelf].book[0..\u003c2])`|[The first two books.](/Tests/RBBJSONTests/READMETests.swift#L141-L154)|\n|`$..book[?(@.isbn)]`|`json[any: .descendantOrSelf].book[has: \\.isbn]`|[All books with an ISBN number.](/Tests/RBBJSONTests/READMETests.swift#L159-L174)|\n|`$..book[?(@.price\u003c10)]`|`json.store.book[matches: { $0.price \u003c= 10 }]`|[All books cheaper than `10`.](/Tests/RBBJSONTests/READMETests.swift#L179-L193)|\n|`$.store[\"book\", \"bicycle\"]..[\"price\", \"author\"]`|`json.store[\"book\", \"bicycle\"][any: .descendantOrSelf][\"price\", \"author\"]`|[The author (where available) and price of every book or bicycle.](/Tests/RBBJSONTests/READMETests.swift#L203-L223)|\n\nOnce you query a JSON value using one of the higher order selectors, the resulting type of the expression will be a lazy `RBBJSONQuery`:\n\n```swift\njson.store.book[0][\"title\"]     // RBBJSON.string(\"Sayings of the Century\")\njson.store.book[0, 1][\"title\"]  // some RBBJSONQuery\n```\n\nBecause `RBBJSONQuery` conforms to `Sequence`, you can initialize an `Array` with it to obtain the results or use e.g. `compactMap`:\n\n```swift\nString(json.store.book[0].title)                    // \"Sayings of the Century\"\njson.store.book[0, 1].title.compactMap(String.init) // [\"Sayings of the Century\", \"Sword of Honour\"]\n\nString(json.store.book[0][\"invalid Property\"])                    // nil\njson.store.book[0, 1][\"invalid Property\"].compactMap(String.init) // []\n```\n\n---\n\n\n\n[jsonpath]: https://goessner.net/articles/JsonPath/\n[dml]: https://oleb.net/blog/2018/06/dynamic-member-lookup/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobb%2Frbbjson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frobb%2Frbbjson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobb%2Frbbjson/lists"}