0

I have inherited a number of javascript files containing a mixture of JSON objects and other js declarations (functions, variables). I'm writing a python script to move the data contained in the JSON objects into another system.

I'd like to avoid duplicating any sort of parser functionality in what is essentially a throw away script and avoid combing through the files by hand.

Is there a nice (pythonic) solution to filter out the JSON in these files and pass it to json.loads() ?

1
  • Why not use some kind of javascript engine, possibly with a python bridge, to access the new objects created (which are just "plain") objects, then output those as json? Commented May 1, 2012 at 9:25

2 Answers 2

2

You can use regular expressions to isolate those JSON objects you are interested in from your available files. Here is a relevant discussion

Sign up to request clarification or add additional context in comments.

Comments

0

If the data just contained json then probably, but if it contains other javascript declarations then it won't be considered well-formed json

2 Comments

Right. I'm looking for a nice way filter out only the JSON.
@John Carter I never used phython, but I never heard of a way in any language to extract json among other javascript declarations

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.