Parsing JSON using C

CJson

C Problem Overview


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?

C Solutions


Solution 1 - C

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.

Solution 2 - C

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.

Solution 3 - C

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

https://github.com/zserge/jsmn

Solution 4 - C

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

Solution 5 - C

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

Solution 6 - C

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.

Solution 7 - C

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondshipperView Question on Stackoverflow
Solution 1 - CMerlyn Morgan-GrahamView Answer on Stackoverflow
Solution 2 - CNateSView Answer on Stackoverflow
Solution 3 - CPrabhpreetView Answer on Stackoverflow
Solution 4 - CYaroslav StavnichiyView Answer on Stackoverflow
Solution 5 - CTantrajJaView Answer on Stackoverflow
Solution 6 - CAlex ReynoldsView Answer on Stackoverflow
Solution 7 - CR.. GitHub STOP HELPING ICEView Answer on Stackoverflow