Code Monkey home page Code Monkey logo

php-json-bender's Introduction

php-json-bender

Simple library to convert json strings to php array declarations.

Table of Contents

Installation

composer require kristijorgji/php-json-bender

Example

Run in the console:

vendor/bin/phpJsonBender /srv/input/test.json /srv/output/test.php

First argument is the real path of the input json.

Second argument is the real path of the desired output.

In my example the content of the input file was as follows:

[{
  "created_at": "Thu Jun 22 21:00:00 +0000 2017",
  "id": 877994604561387500,
  "id_str": "877994604561387520",
  "text": "Creating a Grocery List Manager Using Angular, Part 1: Add & Display Items https://t.co/xFox78juL1 #Angular",
  "truncated": false,
  "entities": {
    "hashtags": [{
      "text": "Angular",
      "indices": [103, 111]
    }],
    "symbols": [],
    "user_mentions": [],
    "urls": [{
      "url": "https://t.co/xFox78juL1",
      "expanded_url": "http://buff.ly/2sr60pf",
      "display_url": "buff.ly/2sr60pf",
      "indices": [79, 102]
    }]
  },
  "source": "<a href=\"http://bufferapp.com\" rel=\"nofollow\">Buffer</a>",
  "user": {
    "id": 772682964,
    "id_str": "772682964",
    "name": "SitePoint JavaScript",
    "screen_name": "SitePointJS",
    "location": "Melbourne, Australia",
    "description": "Keep up with JavaScript tutorials, tips, tricks and articles at SitePoint.",
    "url": "http://t.co/cCH13gqeUK",
    "entities": {
      "url": {
        "urls": [{
          "url": "http://t.co/cCH13gqeUK",
          "expanded_url": "http://sitepoint.com/javascript",
          "display_url": "sitepoint.com/javascript",
          "indices": [0, 22]
        }]
      },
      "description": {
        "urls": []
      }
    },
    "protected": false,
    "followers_count": 2145,
    "friends_count": 18,
    "listed_count": 328,
    "created_at": "Wed Aug 22 02:06:33 +0000 2012",
    "favourites_count": 57,
    "utc_offset": 43200,
    "time_zone": "Wellington"
  }
}]

The generated output php file at /srv/output/test.php is as follows:

<?php
$array = [
    '0' => [
        'created_at' => 'Thu Jun 22 21:00:00 +0000 2017',
        'id' => 877994604561387500,
        'id_str' => '877994604561387520',
        'text' => 'Creating a Grocery List Manager Using Angular, Part 1: Add &amp; Display Items https://t.co/xFox78juL1 #Angular',
        'truncated' => false,
        'entities' => [
            'hashtags' => [
                '0' => [
                    'text' => 'Angular',
                    'indices' => [
                        '0' => 103,
                        '1' => 111,
                    ],
                ],
            ],
            'symbols' => [],
            'user_mentions' => [],
            'urls' => [
                '0' => [
                    'url' => 'https://t.co/xFox78juL1',
                    'expanded_url' => 'http://buff.ly/2sr60pf',
                    'display_url' => 'buff.ly/2sr60pf',
                    'indices' => [
                        '0' => 79,
                        '1' => 102,
                    ],
                ],
            ],
        ],
        'source' => '<a href="http://bufferapp.com" rel="nofollow">Buffer</a>',
        'user' => [
            'id' => 772682964,
            'id_str' => '772682964',
            'name' => 'SitePoint JavaScript',
            'screen_name' => 'SitePointJS',
            'location' => 'Melbourne, Australia',
            'description' => 'Keep up with JavaScript tutorials, tips, tricks and articles at SitePoint.',
            'url' => 'http://t.co/cCH13gqeUK',
            'entities' => [
                'url' => [
                    'urls' => [
                        '0' => [
                            'url' => 'http://t.co/cCH13gqeUK',
                            'expanded_url' => 'http://sitepoint.com/javascript',
                            'display_url' => 'sitepoint.com/javascript',
                            'indices' => [
                                '0' => 0,
                                '1' => 22,
                            ],
                        ],
                    ],
                ],
                'description' => [
                    'urls' => [],
                ],
            ],
            'protected' => false,
            'followers_count' => 2145,
            'friends_count' => 18,
            'listed_count' => 328,
            'created_at' => 'Wed Aug 22 02:06:33 +0000 2012',
            'favourites_count' => 57,
            'utc_offset' => 43200,
            'time_zone' => 'Wellington',
        ],
    ],
];

License

php-json-bender is released under the MIT Licence. See the bundled LICENSE file for details.

php-json-bender's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.