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() ?

John Carter
  • 6,426
  • 2
  • 29
  • 49
  • 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? – Marcin May 01 '12 at 09:25

2 Answers2

2

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

Community
  • 1
  • 1
A_A
  • 2,208
  • 15
  • 25
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

James Fair
  • 580
  • 1
  • 7
  • 18