Skip to main content

No project description provided

Project description

csonpath

that's not my path, that's not your path, but csonpath

Project Sandbox

csonpath is a partial JSONPath implementation in C, with Python bindings. It allows you to query, update, and remove data from JSON objects using path expressions.

🌐 Links

📄 Table of Contents

🚀 Features

  • Find First: Retrieve the first value matching a path.
  • Find All: Retrieve all values matching a path.
  • Update or Create: Update values or create new ones according to the path.
  • Remove: Remove values matching a path.
  • Update or Create Callback: Update values or create new ones according to the path, using a user-define callback.
  • Callback: Call a callback according to the path.
  • Supports both C (using json-c) and Python objects.

✅ Getting Started

Prerequisites

  • C compiler (gcc/clang)
  • json-c library for C usage
  • Python 3.x (for Python bindings)

Build

To build the C tests and examples:

make

Usage (json-c)

See examples in tests/json-c/get-a.c:

#include "csonpath_json-c.h"

const char *json_str = "{ \"a\": \"value\", \"array\": [1,2,3] }";
struct csonpath p;
struct json_object *jobj = json_tokener_parse(json_str);
struct json_object *ret;

csonpath_init(&p, "$.a");
ret = csonpath_find_first(&p, jobj);
// ret now points to "value"

Usage (Python)

import csonpath

d = { "a": "value", "array": [1,2,3] }
o = csonpath.CsonPath("$.a")
print(o.find_first(d))  # Output: "value"

Avaible methodes:

  • CsonPath(path): create a new csonpath object.
  • OBJ.find_first(self, json): take a json, return first found occurrence.
  • OBJ.find_all(self, json): take a json, return all found occurrence in a list.
  • OBJ.remove(self, json): take a json, remove all found occurrence from it.
  • OBJ.update_or_create(self, json, value): take a json, replace all found occurrence from it, by the value, if doesn't found something, create it.
  • OBJ.update_or_create_callback(self, json, callback, callback_data): take a json, if it doesn't find a parent object, create it, then it calls a callback.
  • OBJ.callback(self, json, callback, callback_data): take a json, call a callback, an all occurrence.

callback is a function that take 4 arguments: parent, idx, cur_value and callback_data

Check tests for more examples

Backends and Direct Usage

csonpath is inspired by JSONPath, but is designed to be backend-agnostic: it can work with any C library or environment that manipulates array, object, and scalar types—not just JSON. Out of the box, backends for Python and json-c are provided, but you can create your own backend for other formats such as YAML, or any custom data structure.

To use csonpath with a different data type or library, simply define the required macros and types in a header file (similar to csonpath_json-c.h or csonpath_python.c), then include your backend header before csonpath.h. This approach allows you to adapt csonpath for manipulating data in any format that supports array/object semantics, giving you full flexibility beyond just JSON.

Directory Structure

  • csonpath.h, csonpath_do.h - Main implementation files
  • csonpath_json-c.h - Json-C implementation, use it by including "csonpath_json-c.h" directly in your source
  • csonpath_python.c - python implementation, use it like a python lib (so pip install .)
  • tests/ - Contains tests
  • bench// - Some benchmarks

🤝 Contributing

We welcome contributions!

Please read our Contributing Guidelines and Code of Conduct before submitting a pull request.

Feel free to open issues or pull requests!

📜 License

BSD 3-Clause. See LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

csonpath-0.0.3.tar.gz (11.5 kB view details)

Uploaded Source

File details

Details for the file csonpath-0.0.3.tar.gz.

File metadata

  • Download URL: csonpath-0.0.3.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for csonpath-0.0.3.tar.gz
Algorithm Hash digest
SHA256 72f6e57cfb64f03042fab1a0411cb594065c15667db39650b36d519eb9c364a4
MD5 52d76a3640fce95d0217d75075b5299f
BLAKE2b-256 f6749f2edddaca1ecd47ab776b1c790c6ec1d804276e0b4936a05f9cd37f0f7a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page