Programming language: Objective-C
License: BSD 3-clause "New" or "Revised" License
Tags: JSON     Parsing    
Latest version: v5.0.3

SBJson alternatives and similar libraries

Based on the "JSON" category.
Alternatively, view SBJson alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of SBJson or a related project?

Add another 'JSON' Library


SBJson 5

Chunk-based JSON parsing and generation in Objective-C.

CircleCI Project Status: Inactive - The project has reached a stable, usable state but is no longer being actively developed; support/maintenance will be provided as time allows. Carthage compatible


SBJson's number one feature is stream/chunk-based operation. Feed the parser one or more chunks of UTF8-encoded data and it will call a block you provide with each root-level document or array. Or, optionally, for each top-level entry in each root-level array.

With this you can reduce the apparent latency for each download/parse cycle of documents over a slow connection. You can start parsing and return chunks of the parsed document before the full document has downloaded. You can also parse massive documents bit by bit so you don't have to keep them all in memory.

SBJson maps JSON types to Objective-C types in the following way:

JSON Type Objective-C Type
null NSNull
string NSString
array NSMutableArray
object NSMutableDictionary
true -[NSNumber numberWithBool: YES]
false -[NSNumber numberWithBool: NO]
number NSNumber
  • Booleans roundtrip properly even though Objective-C doesn't have a dedicated class for boolean values.
  • Integers use either long long or unsigned long long if they fit, to avoid rounding errors. For all other numbers we use the double type, with all the potential rounding errors that entails.

"Plain" Chunk Based Parsing

First define a simple block & an error handler. (These are just minimal examples. You should strive to do something better that makes sense in your application!)

SBJson5ValueBlock block = ^(id v, BOOL *stop) {
    BOOL isArray = [v isKindOfClass:[NSArray class]];
    NSLog(@"Found: %@", isArray ? @"Array" : @"Object");

SBJson5ErrorBlock eh = ^(NSError* err) {
    NSLog(@"OOPS: %@", err);

Then create a parser and add data to it:

id parser = [SBJson5Parser parserWithBlock:block

id data = [@"[true," dataWithEncoding:NSUTF8StringEncoding];
[parser parse:data]; // returns SBJson5ParserWaitingForData

// block is not called yet...

// ok, now we add another value and close the array

data = [@"false]" dataWithEncoding:NSUTF8StringEncoding];
[parser parse:data]; // returns SBJson5ParserComplete

// the above -parse: method calls your block before returning.

Alright! Now let's look at something slightly more interesting.

Handling multiple documents

This is useful for something like Twitter's feed, which gives you one JSON document per line. Here is an example of parsing many consequtive JSON documents, where your block will be called once for each document:

id parser = [SBJson5Parser multiRootParserWithBlock:block

// Note that this input contains multiple top-level JSON documents
id data = [@"[]{}" dataWithEncoding:NSUTF8StringEncoding];
[parser parse:data];
[parser parse:data];

The above example will print:

Found: Array
Found: Object
Found: Array
Found: Object

Unwrapping a gigantic top-level array

Often you won't have control over the input you're parsing, so can't use a multiRootParser. But, all is not lost: if you are parsing a long array you can get the same effect by using an unwrapRootArrayParser:

id parser = [SBJson5Parser unwrapRootArrayParserWithBlock:block

// Note that this input contains A SINGLE top-level document
id data = [@"[[],{},[],{}]" dataWithEncoding:NSUTF8StringEncoding];
[parser parse:data];

Other features

  • For safety there is a max nesting level for all input. This defaults to 32, but is configurable.
  • The writer can sort dictionary keys so output is consistent across writes.
  • The writer can create human-readable output, with newlines and indents.
  • You can install SBJson v3, v4 and v5 side-by-side in the same application. (This is possible because all classes & public symbols contains the major version number.)

A word of warning

Stream based parsing does mean that you lose some of the correctness verification you would have with a parser that considered the entire input before returning an answer. It is technically possible to have some parts of a document returned as if they were correct but then encounter an error in a later part of the document. You should keep this in mind when considering whether it would suit your application.

American Fuzzy Lop

I've run AFL on the sbjson binary for over 24 hours, with no crashes found. (I cannot reproduce the hangs reported when attempting to parse them manually.)

                       american fuzzy lop 2.35b (sbjson)

โ”Œโ”€ process timing โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€ overall results โ”€โ”€โ”€โ”€โ”€โ”
โ”‚        run time : 1 days, 0 hrs, 45 min, 26 sec      โ”‚  cycles done : 2      โ”‚
โ”‚   last new path : 0 days, 0 hrs, 5 min, 24 sec       โ”‚  total paths : 555    โ”‚
โ”‚ last uniq crash : none seen yet                      โ”‚ uniq crashes : 0      โ”‚
โ”‚  last uniq hang : 0 days, 2 hrs, 11 min, 43 sec      โ”‚   uniq hangs : 19     โ”‚
โ”œโ”€ cycle progress โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€ map coverage โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  now processing : 250* (45.05%)     โ”‚    map density : 0.70% / 1.77%         โ”‚
โ”‚ paths timed out : 0 (0.00%)         โ”‚ count coverage : 3.40 bits/tuple       โ”‚
โ”œโ”€ stage progress โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€ findings in depth โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  now trying : auto extras (over)    โ”‚ favored paths : 99 (17.84%)            โ”‚
โ”‚ stage execs : 603/35.6k (1.70%)     โ”‚  new edges on : 116 (20.90%)           โ”‚
โ”‚ total execs : 20.4M                 โ”‚ total crashes : 0 (0 unique)           โ”‚
โ”‚  exec speed : 481.9/sec             โ”‚   total hangs : 44 (19 unique)         โ”‚
โ”œโ”€ fuzzing strategy yields โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€ path geometry โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚   bit flips : 320/900k, 58/900k, 5/899k             โ”‚    levels : 8          โ”‚
โ”‚  byte flips : 0/112k, 4/112k, 3/112k                โ”‚   pending : 385        โ”‚
โ”‚ arithmetics : 66/6.24M, 0/412k, 0/35                โ”‚  pend fav : 1          โ”‚
โ”‚  known ints : 5/544k, 0/3.08M, 0/4.93M              โ”‚ own finds : 554        โ”‚
โ”‚  dictionary : 0/0, 0/0, 29/1.83M                    โ”‚  imported : n/a        โ”‚
โ”‚       havoc : 64/300k, 0/0                          โ”‚ stability : 100.00%    โ”‚
โ”‚        trim : 45.19%/56.5k, 0.00%                   โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
^Cโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜             [cpu: 74%]

+++ Testing aborted by user +++
[+] We're done here. Have a nice day!

API Documentation

Please see the API Documentation for more details.



The preferred way to use SBJson is by using CocoaPods. In your Podfile use:

pod 'SBJson', '~> 5.0.0'


SBJson is compatible with Carthage. Follow the Getting Started Guide for iOS.

github "SBJson/SBJson" == 5.0.2

Bundle the source files

An alternative that I no longer recommend is to copy all the source files (the contents of the Classes folder) into your own Xcode project.



  • Review (or create) StackOverflow questions tagged with SBJson if you have questions about how to use the library.
  • Use the issue tracker if you have found a bug.
  • I regret I'm only able to support the current major release.

Philosophy on backwards compatibility

SBJson practice Semantic Versioning, which means we do not break the API in major releases. If something requires a backwards-incompatible change, we release a new major version. (Hence why a library of less than 1k lines has more major versions than Emacs.)

I also try support a gradual migration from one major version to the other by allowing the last three major versions to co-exist in the same app without conflicts. The way to do this is putting the major version number in all the library's symbols and file names. So if v6 ever comes out, the SBJson5Parser class would become SBJson6Parser, etc.


BSD. See [LICENSE](LICENSE) for details.

*Note that all licence references and agreements mentioned in the SBJson README section above are relevant to that project's source code only.