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 🙏

© 2024 – Pkg Stats / Ryan Hefner

jsontophp

v1.0.1

Published

`JSON To PHP` is an npm package that converts JSON strings into PHP-style associative arrays. It is useful when working with JavaScript applications that need to generate PHP code for dynamic content or server-side processing.

Downloads

8

Readme

JSON To PHP

JSON To PHP is an npm package that converts JSON strings into PHP-style associative arrays. It is useful when working with JavaScript applications that need to generate PHP code for dynamic content or server-side processing.

Installation

You can install the package via npm:

npm install jsontophp

Options:

interface Options {
    beautify?: boolean;
    indentLevel?: number;
    indentSize?: number;
    useTabs?: boolean;
}

The Options interface allows you to customize the behavior of the jsonToPhp function. Below are the available properties:

  • beautify (optional, boolean): When set to true, the PHP array is formatted with line breaks and indentation for readability. Default is false.
  • indentLevel (optional, number): Specifies the initial indentation level for the PHP array. Default is 0.
  • indentSize (optional, number): Specifies the number of spaces for indentation when beautify is set to true. Ignored when useTabs is true Default is 4.
  • useTabs (optional, boolean): When set to true, tabs are used for indentation instead of spaces. Default is false.

Usage

After installation, you can import and use jsonToPhp to convert JSON strings into PHP array format.

Basic Conversion

The jsonToPhp function takes a JSON string and converts it to a PHP-style associative array.

import {jsonToPhp} from 'jsontophp';

const jsonString = '{"name": "John", "age": 30}';

const phpArray = jsonToPhp(jsonString);
console.log(phpArray);
// ['name' => 'John', 'age' => 30]

Nested JSON Conversion

The package supports nested JSON objects, converting them into nested PHP arrays.

const nestedJson = '{"user": {"name": "Alice", "email": "[email protected]"}, "active": true}';

console.log(jsonToPhp(nestedJson));
// ['user' => ['name' => 'Alice', 'email' => '[email protected]'], 'active' => true]

Arrays in JSON

You can also convert JSON arrays into PHP arrays.

const jsonArray = '{"fruits": ["apple", "banana", "cherry"]}';

console.log(jsonToPhp(jsonArray));
// ['fruits' => ['apple', 'banana', 'cherry']]

Beautifying Output

You can beautify the output using the beautify option, which adds line breaks and indentation to make the PHP array more readable.

const jsonString = '{"name": "John", "address": {"city": "New York", "zip": "10001"}}';

console.log(jsonToPhp(jsonString, {beautify: true}));
// [
//     'name' => 'John',
//     'address' => [
//         'city' => 'New York',
//         'zip' => '10001'
//     ]
// ]

Indentation Level

The indentLevel option allows you to specify the inital indentation level for the PHP array.

console.log(jsonToPhp(jsonString, {beautify: true, indentLevel: 2}));
//        [
//            'name' => 'John',
//            'address' => [
//                'city' => 'New York',
//                'zip' => '10001'
//            ]
//        ]

You can adjust the indentation size:

console.log(jsonToPhp(jsonString, {beautify: true, indentSize: 2}));
// [
//   'name' => 'John',
//   'address' => [
//     'city' => 'New York',
//     'zip' => '10001'
//   ]
// ]

Using Tabs for Indentation

You can use tabs for indentation by setting the useTabs option to true.

console.log(jsonToPhp(jsonString, {beautify: true, useTabs: true}));
// [
// 	'name' => 'John',
// 	'address' => [
// 		'city' => 'New York',
// 		'zip' => '10001'
// 	]
// ]

Handling Different Data Types

The package supports different JSON data types including strings, numbers, booleans, and null values.

const jsonWithTypes = '{"isAdmin": true, "balance": 100.5, "preferences": null}';

console.log(jsonToPhp(jsonWithTypes));
// ['isAdmin' => true, 'balance' => 100.5, 'preferences' => null]

Edge Cases

Empty JSON

If you provide an empty JSON string, the package will return an empty PHP array.

console.log(jsonToPhp('{}'));
// []

Invalid JSON

If the provided string is not valid JSON, it will throw an error.

try {
    jsonToPhp('{invalid}');
} catch (error) {
    console.error('Invalid JSON format');
}

License

MIT License

Copyright (c) 2024 Andrei Surdu

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.