20

I'd like to read JSON-encoded data into C structs. The structure of the json data is known in advance, relatively flat and mimicked by some C struct typedefs. An array at the third level or so contains an extremely lengthy list of JSON objects which have to be processed one at a time.

The code is intended to run on a very constrained system so the library should not dynamically allocate memory.

I know there is Crockford's List of JSON libraries, but I'm not quite sure which one is the best fit for the stated problem.

wnrph
  • 3,293
  • 4
  • 26
  • 38
  • @alexis thanks for pointing that out. What I was actually asking for is not a recommendation, but a _comparison_. – wnrph May 20 '12 at 15:14
  • I've used this one http://www.digip.org/jansson/ but not others, so no comparisons possible – ShinTakezou May 20 '12 at 15:16
  • @artistoex Actually that's worse, because then it gets argumentative. Either way… I don't really share the opinion of that guy on Meta, but you're probably better off answering this question for yourself using Google. – Potatoswatter May 20 '12 at 15:17
  • 1
    @Potatoswatter Argumentative? Not necessarily. E.g. the intended approach is not very arguable. That is, is a given library designed for reading unknown JSON data, or is it designed for reading JSON data which meet a very known structure into user-defined C structs? The latter is what I am looking for. – wnrph May 20 '12 at 15:48
  • @artistoex That's not clear from the question. Providing more detail would really help. The reason for the downvotes is that it seems you're just asking which is best in an overall sense. (Note, I didn't downvote.) – Potatoswatter May 20 '12 at 16:34
  • Well, at least it looks like you're [in good company](http://chat.stackoverflow.com/transcript/message/3803943#3803943). – Potatoswatter May 22 '12 at 15:33
  • Some of the pitfalls are whether you will want to generate JSON as well as parse it, whether you want to parse certain set JSON data fields into C structs or parse arbitrary JSON data trees, whether you want to parse from streams or only from strings which will fit in memory, whether you want to parse from encodings other than UTF-8, whether you want to convert things like number formats and Unicode escape sequences yourself. – hippietrail Mar 09 '13 at 11:51
  • Related: [Parsing JSON using C](http://stackoverflow.com/questions/6673936) – hippietrail Mar 18 '13 at 02:31

2 Answers2

23

Try jsmn lib, I love that it can parse any json file with only two malloc's.

jsmn is a minimalistic library for parsing JSON data format. It can be easily used in small projects or can be integrated into embedded systems.

jsmn is a good choice, because:

  • it is compatible with C89 compiler version
  • it uses no dynamic memory allocation
  • it has the smallest possible overhead
  • it needs only one pass to parse JSON data
  • it has no dependencies, even libc
  • it is distributed under MIT license, so you can use it in your proprietary projects
Community
  • 1
  • 1
jimon
  • 331
  • 2
  • 4
  • 3
    jsmn doesn't even require mallocs since you can declare arrays, but the drawback is if you might want to parse something very large or don't know the size in advance then you could end up having to allocate or reallocate more memory as you go. – hippietrail Mar 09 '13 at 11:53
  • 2
    @hippietrail This drawback is a consequence of using `C`, not `jsmn`. – yyny Sep 08 '16 at 22:29
  • @YoYoYonnY: Indeed. It looks like I was just providing a caveat to jimon's opening claim. – hippietrail Sep 09 '16 at 04:26
  • 1
    @hippietrail I see, unfortunate wording by @jimon. However, after reading the `jsmn` source code, it looks as if the most efficient way to use `jsmn` is indeed to run the parser twice, be it with just one malloc required, not two. – yyny Sep 09 '16 at 10:15
  • jsmn++ this is software done the right way. – aaa90210 Feb 08 '17 at 04:43
4

Try with json-c is one of the most common and it is open source and work also on Windows (Win32).

JSON-C implements a reference counting object model that allows you to easily construct JSON objects in C, output them as JSON formatted strings and parse JSON formatted strings back into the C representation of JSON objects.

Zaman
  • 130
  • 2
  • 8
aleroot
  • 71,077
  • 30
  • 176
  • 213