npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@jlarky/json-to-javascript

v0.1.0

Published

Convert JSON data to JavaScript code literals with smart handling of multiline strings.

Readme

json-to-javascript

Convert JSON data to JavaScript code literals with smart handling of multiline strings.

This tool transforms JSON into properly formatted JavaScript code, automatically converting multiline strings into template literals with dedent support for clean indentation.

CLI Usage

Example: Converting JSON with Multiline Strings

Input file (input.json):

{
  "greeting": "Hello\nWorld",
  "message": "Line 1\nLine 2\nLine 3",
  "count": 42
}

Node.js:

npx @jlarky/json-to-javascript \
  --inputFile input.json \
  --outputFile output.ts \
  --useDedent true \
  --prefix "import dedent from 'dedent'; export const data = (" \
  --suffix ") as const" \
  --prettierOptions '{"parser":"babel-ts"}'

Bun:

bunx @jlarky/json-to-javascript \
  --inputFile input.json \
  --outputFile output.ts \
  --useDedent true \
  --prefix "import dedent from 'dedent'; export const data = (" \
  --suffix ") as const" \
  --prettierOptions '{"parser":"babel-ts"}'

Deno:

deno run --allow-sys=cpus --allow-env --allow-read=. --allow-write=. \
  jsr:@jlarky/json-to-javascript/cli \
  --inputFile input.json \
  --outputFile output.ts \
  --useDedent true \
  --prefix "import dedent from 'dedent'; export const data = (" \
  --suffix ") as const" \
  --prettierOptions '{"parser":"babel-ts"}'

Output file (output.ts):

import dedent from "dedent";
export const data = {
  greeting: dedent`
    Hello
    World
  `,
  message: dedent`
    Line 1
    Line 2
    Line 3
  `,
  count: 42,
} as const;

Basic Usage

npx @jlarky/json-to-javascript \
  --inputFile input.json \
  --outputFile output.js

This outputs the same JSON with default prefix ( and suffix ):

({
  greeting: "Hello\nWorld",
  message: "Line 1\nLine 2\nLine 3",
  count: 42,
});

The difference from the previous example: no custom prefix/suffix, import statement, or dedent - just the data wrapped in parentheses with escaped newlines.

CLI Options

json-to-javascript [options]

Required:
  --inputFile <path>              Input JSON file
  --outputFile <path>             Output JavaScript file

Optional:
  --prefix <string>               Prefix for output (default: "(")
  --suffix <string>               Suffix for output (default: ")")
  --usePrettier <boolean>         Format with Prettier (default: true)
  --prettierOptions <json>        Prettier options as JSON string
  --useDedent <boolean>           Convert multiline strings to template literals
  --dedentPrefix <string>         Prefix for dedent (default: " dedent")
  --dedentSuffix <string>         Suffix for dedent (default: "")
  --jsonStringifySpace <number>   Indentation spaces for JSON.stringify
  --help                          Show help

Installation

If you want to use the library in your code or install it locally:

If using Deno:

deno add jsr:@jlarky/json-to-javascript

If using Bun:

bunx jsr add @jlarky/json-to-javascript # or bun add @jlarky/json-to-javascript

If using Node.js:

npx jsr add @jlarky/json-to-javascript # or npm install @jlarky/json-to-javascript

Library Usage

import { jsonToJavascript } from "@jlarky/json-to-javascript";

const data = { name: "John", age: 30 };
const result = await jsonToJavascript(data);
console.log(result.code); // ({ name: "John", age: 30 });

Use Cases

  • Generate configuration files from JSON
  • Create test fixtures from JSON data
  • Convert API responses to TypeScript constants
  • Generate code with proper template literal formatting for multiline content

Development

See CONTRIBUTING.md for development setup, testing, and publishing guidelines.

License

MIT