LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-16-2015, 02:40 PM   #1
cin_
Member
 
Registered: Dec 2010
Posts: 281

Rep: Reputation: 24
Parsing Javascript To JSON Using Python 3


this is a very specific request, and for that i apologise, but i am at a loss for what to do..

for a javascript project i am working on i want to be able to parse javascript with python and i found this implementation`port of the original narcissus called pynarcissus:

https://github.com/jtolds/pynarcissus

it is a straightforward tool: clone into folder, then run the "./jsparser3.py /SOME/JS/FILE.js", and stdout shows the class structure of the parsed js

the problem for me is the information is buried in the python class structure.. something with which i thought i had a solid understanding thereof but now am thinking i am only vaguely competent in.. and i want the output to be in JSON

i have tried to mine the data out but each time the JSON is invalid

my question is how would you go about doing something like this? i'd appreciate any specifics because the project contains nested classes of disparate types creating what seems to be a wholly unique problem

here are my attempts:

* i took the return value for parse() and created a function that descends through the class structure returning values based on their type: 'str', 'int', 'bool', 'NoneType', 'list', 'dict', '__main__.Node', '__main__.Tokenizer', '__main__.Object'; but the returned object is missing some properties in the classes, ie 'type', while retaining others like 'type_', also tokenizer always contains the same values

* i took the output of the __str__ function that the program prints to stdout, removed the clear and copy functions and the tokenizer: [object Object], then tried to manually add in double quotes where necessary to make the output a valid JSON object.. a few problems here, first off ignoring the tokenizer object seems like i am missing out on vital information, and the other problem was that sometimes there is "value" : "{" and sometimes there is "value" : { .. }, after completing the work the JSON was invalid

* assuming the issue lied in the "value" : { .. } issue i resolved to add a new function identical the the __str__ function but instead of printing just s values, it would print "s" where necessary.. now, i could differentiate between "value": "{" and "value" : "{ .. }" but i would have to go through and manually remove the quotes around objects.. after doing so the JSON was invalid

* i've tried every copy'pasta solution for python class to json from the stacks but the nested aspect of the classes along with the changing types add complexity.. some properties lack a .__dict__ even when the type is "<class 'dict'>" so the one'method'fits'all lambda solutions fail


..
i asked on the stacks :: http://stackoverflow.com/questions/3...output-to-json
as well as placed an issue :: https://github.com/jtolds/pynarcissus/issues/6

thanks for the time and thought, here's hoping..

Last edited by cin_; 05-18-2015 at 01:03 PM. Reason: gramm`err
 
Old 05-17-2015, 09:55 AM   #2
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 399

Rep: Reputation: Disabled
I guess the simple answer is you need to extend JSONEncoder, overload default and convert the non-serializable objects to something that is serializable.
 
Old 05-17-2015, 03:54 PM   #3
cin_
Member
 
Registered: Dec 2010
Posts: 281

Original Poster
Rep: Reputation: 24
figured

closed the issue and added a pull request

https://github.com/jtolds/pynarcissus/issues/6

you can check out the fork if you got here by looking for a way to parse javascript to json in python 3

https://github.com/ghissues/pynarcissus

i only added the changes to the python3 implementation

Last edited by cin_; 05-17-2015 at 03:54 PM. Reason: gramm`err
 
Old 05-18-2015, 04:08 PM   #4
cin_
Member
 
Registered: Dec 2010
Posts: 281

Original Poster
Rep: Reputation: 24
hopeful

once i massaged the code of pynarcissus to print json i found that the process fails on nested functions.. hilariously exactly the reason i stepped away from my homebrew method

in another thread(i) @firstfire suggested esprima and pyesprima

so i looked into the esprima suggestions, and after a bit of 2to3`ing and some more work returning valid json i got it working and so far it fits my needs perfectly

check out the issue, pull request, and fork:

issues : https://github.com/int3/pyesprima/issues/1

pr : https://github.com/int3/pyesprima/pull/2

fork : https://github.com/ghissues/pyesprima


(i) http://www.linuxquestions.org/questi....php?p=5364199

Last edited by cin_; 05-18-2015 at 04:08 PM. Reason: gramm`err
 
  


Reply

Tags
class, javascript, json, parser, python3



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Java HTTP PUT Request/JSON Not Working But Using Python It Does ? metallica1973 Programming 5 04-16-2015 04:13 PM
[SOLVED] Python: load json file and print the values of a field zinon75 Programming 4 03-11-2015 03:33 PM
How to read json files using python danoc93 Programming 1 08-19-2012 02:15 AM
[SOLVED] Python n00b: promoting a derived class to base class? CoderMan Programming 2 03-11-2010 01:46 PM
Class Arrays in Python rootyard Programming 1 02-13-2004 10:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration