106

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?

7
  • 6
    Are you using a linux distro? Commented Jul 13, 2011 at 4:06
  • json-glib Commented Jul 13, 2011 at 4:07
  • Would objective-c library work? I use TouchJson on my mac when in obj-c its very easy to use and small. Commented Jul 13, 2011 at 4: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. Commented Mar 9, 2013 at 11:13
  • Related: Parse JSON in ANSI C [closed] Commented Mar 18, 2013 at 2:31

7 Answers 7

89

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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: 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 :)
@dshipper: I've had good experiences with jansson, which is lightweight and well documented. digip.org/jansson/doc/2.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 :)
So, @dshipper, did you go for sourceforge.net/projects/cjson or what?
|
46

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.

2 Comments

Slow when getting array element by index.
@EnesAltınkaya Yes, because items are stored as a linked list. If your data is large, JSON is the wrong choice anyway.
13

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);

2 Comments

As you seem to be the developer of NXJSON, maybe you could comment on how it compares with cJSON or other options?
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.
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

4 Comments

Direct Github link: github.com/akheron/jansson
I was tempted to use jsmn because of its speed[1], but it does not support encoding. Furthermore Jansson is really easy to use. --- [1] translate.google.it/…
OjC is fast and supports encoding. github.com/ohler55/ojc
Jansson is my go to for parsing JSON in C
11

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

https://github.com/zserge/jsmn

3 Comments

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 */
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.
Apparently they have fixed their "TODO" for Unicode symbols.
10

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.

2 Comments

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.
+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.
10

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.