105

I'm trying to find a good way to parse JSON in C. I really don't need a huge library or anything, I would rather have something small and lightweight with a bare minimum of features, but good documentation.

Does anyone have anything they can point me to?

idmean
  • 14,540
  • 9
  • 54
  • 83
dshipper
  • 3,489
  • 4
  • 30
  • 41
  • 6
    Are you using a linux distro? – Nick Jul 13 '11 at 04:06
  • Would objective-c library work? I use TouchJson on my mac when in obj-c its very easy to use and small. – Nick Jul 13 '11 at 04:12
  • When you say "bare minimum" how minimal can you actually go? Some will handle various Unicode encodings, some just UTF-8, and some only pay attention the the ASCII "format characters" such as `{`, `[`, `]`, `}`, `,`, `"`, and backslash. They may or may not leave turning Unicode escape sequences up to you, they may leave it up to you to check whether numbers are within the allowable Unicode range, etc. Then there's the major difference between parsing arbitrary JSON into trees versus known JSON into C structs. – hippietrail Mar 09 '13 at 11:13
  • Related: [Parse JSON in ANSI C \[closed\]](http://stackoverflow.com/questions/10674575) – hippietrail Mar 18 '13 at 02:31
  • Let me UN-recommend two seemingly reasonable libraries: Both *RapidJSON* and *JSONXX* are completely unusable. RapidJSON will not compile out of the box, and JSONXX fails simple tests. – Meekohi Feb 05 '14 at 22:29
  • [json-glib](http://live.gnome.org/JsonGlib) – Nick Jul 13 '11 at 04:07
  • https://github.com/ajithcofficial/ajson – Ajith C Narayanan Feb 24 '20 at 10:01

7 Answers7

85

Json isn't a huge language to start with, so libraries for it are likely to be small(er than Xml libraries, at least).

There are a whole ton of C libraries linked at Json.org. Maybe one of them will work well for you.

Neuron
  • 5,141
  • 5
  • 38
  • 59
Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
  • 3
    Yea thanks for the answer! I went through a bunch of them and they were pretty poorly documented/maintained. I thought I would try here before I suffered through integrating one.... :) – dshipper Jul 13 '11 at 04:11
  • 2
    @dshipper: http://sourceforge.net/projects/cjson/ looks somewhat promising, given your requirements. Haven't used any of these libraries, personally, so I can't recommend one. Suggestions for a single library only be an opinion, anyhow, and not a definitive answer :) – Merlyn Morgan-Graham Jul 13 '11 at 04:13
  • 5
    @dshipper: I've had good experiences with jansson, which is lightweight and well documented. http://www.digip.org/jansson/doc/2.1/ – Dietrich Epp Jul 13 '11 at 04:21
  • 1
    @Dietrich, dshipper: I looked at Jansson just now. I agree that it is well documented and quite small, and looks like it has a good test suite. It'd probably fit your requirements too. But there are probably several libraries out there that would work. I'd recommend you abstract your Json dependencies as best you can, try out a few libraries, and see which gives you the least grief :) – Merlyn Morgan-Graham Jul 13 '11 at 04:53
  • So, @dshipper, did you go for sourceforge.net/projects/cjson or what? – Mawg says reinstate Monica Apr 22 '15 at 14:54
  • cJSON we use well – hukeping Feb 04 '20 at 07:36
44

cJSON has a decent API and is small (2 files, ~700 lines). Many of the other JSON parsers I looked at first were huge... I just want to parse some JSON.

Edit: We've made some improvements to cJSON over the years.

NateS
  • 5,751
  • 4
  • 49
  • 59
11

Jsmn is quite minimalistic and has only two functions to work with.

https://github.com/zserge/jsmn

Bernardo Ramos
  • 4,048
  • 30
  • 28
Prabhpreet
  • 219
  • 2
  • 3
  • One drawback with jsmn is that it doesn't handle Unicode escape sequences. In its source you will see: `/* Allows escaped symbol \uXXXX */` ... `case 'u':` ... `/* TODO */` – hippietrail Mar 09 '13 at 11:20
  • 15
    Also all it does is give you tokens, good luck making any sense of the data. It does look like the absolute smallest, efficient thing you can do, but it doesn't do a lot either. – NateS Mar 26 '13 at 22:10
  • 1
    Apparently they have fixed their "TODO" for Unicode symbols. – aviggiano Feb 12 '16 at 17:52
11

NXJSON is full-featured yet very small (~400 lines of code) JSON parser, which has easy to use API:

const nx_json* json=nx_json_parse_utf8(code);
printf("hello=%s\n", nx_json_get(json, "hello")->text_value);
const nx_json* arr=nx_json_get(json, "my-array");
int i;
for (i=0; i<arr->length; i++) {
  const nx_json* item=nx_json_item(arr, i);
  printf("arr[%d]=(%d) %ld\n", i, (int)item->type, item->int_value);
}
nx_json_free(json);
Bernardo Ramos
  • 4,048
  • 30
  • 28
Yaroslav Stavnichiy
  • 20,738
  • 6
  • 52
  • 55
  • As you seem to be the developer of NXJSON, maybe you could comment on how it compares with cJSON or other options? – Matt Jan 13 '14 at 09:35
  • 6
    Simple things done simple way - this is main difference of NXJSON from most other parsers. cJSON is also simple, but beyond json parser it also includes json constructor as well as serializer. cJSON needs more memory as it duplicates all strings, while NXJSON does all manipulations in place destroying original content. Depending on your task these differences could be considered either as advantages or disadvantages. NXJSON also handles comments, which is good for parsing config files. – Yaroslav Stavnichiy Jan 14 '14 at 11:36
11

You can have a look at Jansson

The website states the following: Jansson is a C library for encoding, decoding and manipulating JSON data. It features:

  • Simple and intuitive API and data model
  • Can both encode to and decode from JSON
  • Comprehensive documentation
  • No dependencies on other libraries
  • Full Unicode support (UTF-8)
  • Extensive test suite
Marco Sulla
  • 15,299
  • 14
  • 65
  • 100
TantrajJa
  • 1,987
  • 2
  • 15
  • 9
9

I used JSON-C for a work project and would recommend it. Lightweight and is released with open licensing.

Documentation is included in the distribution. You basically have *_add functions to create JSON objects, equivalent *_put functions to release their memory, and utility functions that convert types and output objects in string representation.

The licensing allows inclusion with your project. We used it in this way, compiling JSON-C as a static library that is linked in with the main build. That way, we don't have to worry about dependencies (other than installing Xcode).

JSON-C also built for us under OS X (x86 Intel) and Linux (x86 Intel) without incident. If your project needs to be portable, this is a good start.

Kurtis
  • 1,599
  • 1
  • 16
  • 30
Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
9

Do you need to parse arbitrary JSON structures, or just data that's specific to your application. If the latter, you can make it a lot lighter and more efficient by not having to generate any hash table/map structure mapping JSON keys to values; you can instead just store the data directly into struct fields or whatever.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • 3
    This is a very important point when using JSON with non-dynamic languages such as C. But it's not an answer so should really be a comment to the OP's question. – hippietrail Mar 09 '13 at 11:22
  • +1 for creative thinking! How would that work though? Couldn't this still make use of a particularly light-weight library? Parsing into structure fields still sounds like a fair bit of work. – Jodes Oct 04 '16 at 17:36