SwiftyJSearch extends the functionality of SwiftyJSON with JSON search functionality.
This package extends the use of SwiftyJSON, a JSON handling package for Swift.
Checkout the project here: SwiftyJSON
Changes made to the SwiftyJSON source code are listed in SwiftyJSON Changes
- iOS 13.0+, macOS 10.13+
- Xcode 14
You can use The Swift Package Manager to install SwiftyJSearch by adding the proper description to your Package.swift file:
// swift-tools-version:5.8
import PackageDescription
let package = Package(
name: "YOUR_PROJECT_NAME",
dependencies: [
.package(url: "https://github.com/seanericksonpl12/SwiftyJSearch.git", from: "1.0.0"),
]
)Then run swift build whenever you get prepared.
You can use CocoaPods to install SwiftyJSearch by adding it to your Podfile:
platform :ios, '13.0'
use_frameworks!
target 'MyApp' do
pod 'SwiftyJSON', :git => 'https://github.com/seanericksonpl12/SwiftyJSearch.git'
endimport SwiftyJSearchlet json = JSON(data: someData)For all JSON object usage info, check out the SwiftyJSON Docs
Large JSON data can be difficult to read, especially when deeply nested. SwiftyJSearch makes reading JSON much easier with the treeFormat variable.
Example JSON:
[
{
"car types": [
{
"brand": "Chevy",
"gasoline": true,
"models": [
"Camaro",
"Corvette",
"Silverado",
"Suburban"
]
},
{
"my own car": {
"miles": 91242,
"gasoline": true,
"parts" : [
"steering wheel",
"brake pads",
"chassis"
]
}
}
]
}
]let json = JSON(data: jsonDataFromAbove)
print(json.treeFormat)
// output:
└── car types
├── #
│ ├── models
│ │ ├── Camaro
│ │ ├── Corvette
│ │ ├── Silverado
│ │ └── Suburban
│ ├── gasoline
│ │ └── true
│ └── brand
│ └── Chevy
└── my own car
└── #
├── miles
│ └── 91242
├── gasoline
│ └── true
└── parts
├── steering wheel
├── brake pads
└── chassisTo increase readability, '#' nodes represent dictionaries with more than a single key
Since JSON Dictionaries are unordered, the order of pairs in the dictionary will vary
let json = JSON(data: jsonDataFromAbove)
let values = json.bfs(for: "my own car")
print(values.first!.treeFormat)
// output:
#
├── parts
│ ├── steering wheel
│ ├── brake pads
│ └── chassis
├── miles
│ └── 91242
└── gasoline
└── trueOr search multiple keys with one search:
let json = JSON(data: jsonDataFromAbove)
let valueDictionary = json.bfs(for: ["models", "parts"])
if let parts = valueDictionary["parts"]?.first, let models = valueDictionary["models"]?.first {
print(models.treeFormat)
print(parts.treeFormat)
}
// output:
├── Camaro
├── Corvette
├── Silverado
└── Suburban
├── steering wheel
├── brake pads
└── chassisWorks the same as above, only running the search depth - first.
let json = JSON(data: jsonDataFromAbove)
let values = json.dfs(for: "my own car")
print(values.first!.treeFormat)
// output:
#
├── parts
│ ├── steering wheel
│ ├── brake pads
│ └── chassis
├── miles
│ └── 91242
└── gasoline
└── truePrint out the direct path through the json to a specific key or value.
let json = JSON(data: jsonDataFromAbove)
let path = json.path(to: "steering wheel")
print(path)
// output:
[...] -> car types : [...] -> my own car : parts : [...] -> steering wheelFor an alternate way to store and manipulate JSON Data, you can use the JSONTree structure. JSONTree is a basic tree data structure, but built around storing JSON data.
let json = JSON(data: jsonDataFromAbove)
let tree = JSONTree(json: json)Or build it yourself:
let root = JSONTree.Node(children: [], content: .string("root"))
let tree = JSONTree(root: root)JSONTree nodes store values with the ContentType Enum.
ContentType values:
.string(String)
.number(NSNumber)
.bool(Bool)
.nullThe JSONTree prettyFormat works the same as the JSON treeFormat:
let json = JSON(data: jsonDataFromAbove)
let tree = JSONTree(json: json)
print(tree.prettyFormat)
// output:
└── car types
├── #
│ ├── models
│ │ ├── Camaro
│ │ ├── Corvette
│ │ ├── Silverado
│ │ └── Suburban
│ ├── gasoline
│ │ └── true
│ └── brand
│ └── Chevy
└── my own car
└── #
├── miles
│ └── 91242
├── gasoline
│ └── true
└── parts
├── steering wheel
├── brake pads
└── chassisJSONTree is autobalanced by sorting unordered dictionaries on init to improve search speed, so output between json.treeFormat and tree.prettyFormat may vary
Search for any data in the tree, similar to bfs with json objects, and get a reference to the tree node containing it:
let json = JSON(data: jsonDataFromAbove)
let tree = JSONTree(json: json)
let node = tree.search(for: "chassis")
node?.content = .string("radio")
print(tree.prettyFormat)
// output:
...
└── my own car
└── #
├── miles
│ └── 91242
├── gasoline
│ └── true
└── parts
├── steering wheel
├── brake pads
└── radioModify the json yourself by adding new children to nodes, or removing nodes with the provided functions:
let json = JSON(data: jsonDataFromAbove)
let tree = JSONTree(json: json)
tree.removeAll(where: { $0.content == .string("parts") || $0.content == .number(91242)})
print(tree.prettyFormat)
// output:
...
└── my own car
└── #
├── miles
├── gasoline
│ └── true
├── steering wheel
├── brake pads
└── chassisDue to a lack of updates in the SwiftyJSON project in recent years, this package contains the source code from SwiftyJSON with updated iOS/MacOS version support rather than linking it as a dependency. All changes made to the original SwiftyJSON code are listed below.
All other functionality not listed below remain accurate to the original projects README.
Int8 appears to no longer be a valid value for constructing an NSNumber (see NSNumber). As such, int8 and int8Value have been removed as JSON variables. uInt8 is still valid and available.
SwiftyJSON - SwiftyJSON SwiftyJSON Authors - lingoer, wongzigii, Luke Tang
SwiftyJSearch Author - Sean Erickson [email protected]