Home

Awesome

SMJJSONPath

SMJJSONPath is a complete JSONPath implementation written in Objective-C. It's a wide adaptation of Jayway JsonPath implementation.

Overview

It supports a wide bunch of functionalities:

You can take a look to the Jayway JsonPath documentation for more information.

Adaptation

This implementation is a tight adaptation of Jayway JsonPath. It respects the original structure and naming, as much as possible. The changes are mainly to be more Objective-C stylized (named parameters, use NSError instead of try-catch-exception, etc.).

This tight adaptation was done for different reasons:

This code is currently in sync with the source code in commit 00cfce3.

Query

Simple example:

// Create a SMJJSONPath object
SMJJSONPath *jsonPath = [[SMJJSONPath alloc] initWithJSONPathString:@"$.books..author" error:&error];

// Create a configuration.
SMJConfiguration *configuration = [SMJConfiguration defaultConfiguration];

// Query a JSON document.
NSArray *result = [jsonPath resultForJSONFile:fileURL configuration:configuration error:&error];

// That's all.

Update

You can update a JSON mutable object accordingly to a JSONPath:

// Create a SMJJSONPath object
SMJJSONPath *jsonPath = [[SMJJSONPath alloc] initWithJSONPathString:@"$.books..author" error:&error];

// Create a configuration.
SMJConfiguration *configuration = [SMJConfiguration defaultConfiguration];

// Read a JSON document with mutable containers.
id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];

// Update the json.
[jsonPath updateJSONMutableObject:jsonObject deleteWithConfiguration:configuration error:&error];


// The queried path was deleted in jsonObject.