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. remimu.h is under the CC0 1.0 Universal, original source is here

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.1.0.tar.gz (12.9 kB view details)

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for csonpath-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d919d65ec0a563fbb909f3ab9307a02afbdc09dc8b9901bbc3380b8b09610c75
MD5 c269a846f36c5139b3f5108bcc61dcde
BLAKE2b-256 57aa4873c879042a95c267cce318b94d10e681e1df1ada7740cdaf609d5eff84

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