Code Monkey home page Code Monkey logo

js-object-to-json's People

Contributors

bradietilley avatar mytryk avatar ovidyou avatar vanodevium avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

js-object-to-json's Issues

Problem converting data that has functions inside

Hi! Thank you for you project!
I hope it can help me to parse data from a JS file.
I have a following example of JS data:

var some_data = {data: s("London")};

where s is a translation function used in JS code. I need to get an array or PHP object from that.
Unfortunately, such code can't be processed with the json_decode function that you use under the hood. It says Syntax Error.
If I call convertToJson I get:

{"data":"s(""London")}

Double quote appears, which should not be there. Do you think if there's any workaround for that?

Capitalization Issue in Example

Hi Ovi,

Your example code has an error in the capitalization of the namespace:

// Convert the Javascript object to JSON format
$json = \Ovidigital\JsObjectToJson\JsConverter::convertToJson($jsObjectString);

Should actually be:

// Convert the Javascript object to JSON format
$json = \OviDigital\JsObjectToJson\JsConverter::convertToJson($jsObjectString);

This drove me crazy for about half an hour!

Remove "var/const/let myObject = " and ";" at end of object.

Lets say this is your JS object

constmyObject = {
   "myPropertyA": "myValue",
   "myPropertyB": "myValue"
};

The JS object minified

const myObject={"myPropertyA":"myValue","myPropertyB":"myValue"};

When I use this package, it does not remove the "constmyObject = " or the ";" linebreak at the end of the object.

Please allow for the automatic removal of the object name and the line break at the end of the object.

New parsing issue

Hi

I found a new case when parsing the javascript object fails:

{
    "metadata": "{\"title\":\"Some // Title\"}"
}

The parsed result is NULL.

Version 1.1.2 breaks the parsing

Hi

The changes introduced in the 1.1.2 version breaks the old parsing mechanism.
Here you can find the example code:

<?php

use OviDigital\JsObjectToJson\JsConverter;

include 'vendor/autoload.php';

$input = '{sources:[],image:"http://127.0.0.1/sapsdvcx3kgq.jpg",duration:"3200",width:"100%"}';

var_dump(JsConverter::convertToJson($input));

Output with version 1.1.1:

string(91) "{"sources":[],"image":"http://127.0.0.1/sapsdvcx3kgq.jpg","duration":"3200","width":"100%"}"

Output with version 1.1.2:

string(30) "{"sources":[],"image:""http":""
                                 ^
                                 completly broken

I think the \OviDigital\JsObjectToJson\JsConverter::removeComments() is not production ready.

Issue with quotes

Hi!
Found one more case that does not work.
Data for convertToJson, it's a one-line string:

{id:304,name: "Anna's house",jobs:[{id:3041,title: "Drive to Anna's house.",label: "Drive",steps:3}]}

Expected result:

{"id":304,"name":"Anna's house","jobs":[{"id":3041,"title": "Drive to Anna's house.","label":"Drive","steps":3}]}

Actual result:

{"id":304,"name":"Anna's house\",jobs:[{id:3041,title: \"Drive to Anna's house.","label":"Drive","steps":3}]}

Sample code:

$result = \OviDigital\JsObjectToJson\JsConverter::convertToJson('{id:304,name: "Anna\'s house",jobs:[{id:3041,title: "Drive to Anna\'s house.",label: "Drive",steps:3}]}');
echo $result;

Handle trailing commas

Could you please improve the package to handle this case?

<?php

include 'vendor/autoload.php';

// A variable containing a JavaScript object as a string
$jsObjectString = "{ foo:  'bar', }";

// Convert the Javascript object to JSON format
$json = \OviDigital\JsObjectToJson\JsConverter::convertToJson($jsObjectString);

var_dump($json);

The current output is string(14) "{"foo":"bar",}"

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.