Public
Script
Readme

Diesel

Diesel is a lightweight data manipulation library inspired by Tom Wright's Dasel, designed for easy querying and transformation of various data formats such as JSON, YAML, CSV, and TOML. It allows users to select, update, and delete data using a simple selector syntax.

Heavily adapted from https://github.com/TomWright/dasel

Features

  • Multi-format Support: Works with JSON, YAML, CSV, and TOML.
  • Dynamic Selectors: Use conditions to filter data dynamically.
  • Function Support: Built-in functions for data manipulation (e.g., length, sum, avg).
  • Easy Integration: Can be used in both Deno and Val Town environments.

Usage

import Diesel from "https://esm.town/v/yawnxyz/diesel";

async function main() {
  const jsonData = `
  {
    "users": [
      {"id": 1, "name": "Alice", "age": 30},
      {"id": 2, "name": "Bob", "age": 25},
      {"id": 3, "name": "Charlie", "age": 35}
    ],
    "settings": {
      "theme": "dark",
      "notifications": true
    }
  }
  `;****

  const diesel = new Diesel(jsonData, 'json');
  try {
    console.log("All data:", await diesel.select(''));
    console.log("All users:", await diesel.select('users'));
    console.log("First user's name:", await diesel.select('users.[0].name'));
    console.log("Users over 30:", await diesel.select('users.(age>30)'));
    
    await diesel.put('settings.theme', 'light');
    console.log("Updated settings:", await diesel.select('settings'));
    
    // await diesel.delete('users.[1]');
    // console.log("Users after deletion:", await diesel.select('users'));
    
    console.log("Data in YAML format:");
    console.log(await diesel.convert('yaml'));
    
    console.log("Data in TOML format:");
    console.log(await diesel.convert('toml'));
    
    console.log("Number of users:", await diesel.select('users.length()'));
    console.log("User names in uppercase:", await diesel.select('users.[*].name.toUpper()'));
  } catch (error) {
    console.error("An error occurred:", error);
  }
}

main();

Installation

To use Diesel, simply import it in your Deno project as shown in the usage example.

License

This project is licensed under the MIT License.

Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
September 2, 2024