Skip to main content

Work with JSON which is stored as a value in a JavaScript file

Project description

javascript-data-files

This is a collection of Python functions for manipulating JavaScript "data files" -- that is, JavaScript files that define a single variable with a JSON value.

This is an example of a JavaScript data file:

const shape = { "sides": 5, "colour": "red" };

Think of this module as the JSON module, but for JavaScript files.

These data files are meant to be both human- and machine-readable.

Usage

If you install javascript-data-files:

  • You can read a JavaScript file with read_js(path, varname)
  • You can write a JavaScript file with write_js(path, value, varname)
  • You can append an item to a JavaScript array with append_to_js_array(path, value)
  • You can append a key-value pair to a JavaScript object with append_to_js_object(path, key, value)

If you install javascript-data-files[typed]:

  • You can read a JavaScript file and validate it matches a particular Python type with read_typed_js(path, varname, model).

Installation

You have two options:

  1. Copy the file src/javascript folder into your project. You probably want to copy the tests as well.

  2. Install the package using pip:

    $ pip install javascript-data-files
    

Why not use JSON files?

If you've opening an HTML file from disk, you can load data from a local JavaScript file, for example:

<script src="file://users/alexwlchan/repos/javascript-data-files/data.js"></script>

This is the only way to load data from an external file from an HTML file you've opened locally -- you can't do this with a JSON file, for example.

I have a lot of HTML files and local sites I build with an HTML viewer and metadata in a JavaScript file. The convenience of this approach outweighs the mild annoyance of having to store data in JavaScript, not JSON.

Development

If you want to make changes to the library, there are instructions in CONTRIBUTING.md.

License

MIT.

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

javascript_data_files-1.2.3.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

javascript_data_files-1.2.3-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file javascript_data_files-1.2.3.tar.gz.

File metadata

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

File hashes

Hashes for javascript_data_files-1.2.3.tar.gz
Algorithm Hash digest
SHA256 4ed13e7e12dc5d0056a1fa4af36c07d2270238d8295c63bf4f34072ae27f32a5
MD5 9e18cf1f0ddccf817364f8b993907c17
BLAKE2b-256 5d0f0e6011f090b57a112176985229de043756afebe762aed931e146c8c04efd

See more details on using hashes here.

File details

Details for the file javascript_data_files-1.2.3-py3-none-any.whl.

File metadata

File hashes

Hashes for javascript_data_files-1.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6ea716553b195099c35eea42ad3972cc035b5c313a5335eddc24da42c9123fc1
MD5 dcbfaa5a7662beaba5ba1713ddf1fb1f
BLAKE2b-256 598b6068231d1fb903ec0838b82cd6378d16ef850eab9dca9cf49e3e200d08a9

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