2

NOTE: This is not for web programming. We use javascript to interface with low level hardware, hence let's not go with jQuery APIs etc.

I have a javascript file that performs a sequence of actions on a device, and I have a python file that will be invoked later to validate these actions. There is a set of hardware information hard-coded in both javascript file and python file. I want to avoid this duplication of information by putting these info into a JSON file so both can access it.

// Javascript
var hardware_info = JSON.parse(load('hardware.json'));
// load() is probably not standard javascript API, but it basically copies that code into the existing script.

Already failed by this step because 'hardware.json' is not using javascript syntax...

I already validated the json using jshint/jslint, hardware.json looks like this:

{
    "hardware1": {
        "ID": "xxx"
    },
    "hardware2": {
        "ID": "yyy"
    }
}

The following Python works well for accessing the json, there is not much to it:

with open('hardware.json', 'r') as f:
    data = json.load(f)
return 0
  • 4,226
  • 6
  • 47
  • 72
  • You don't want to load the JSON file as if it's JavaScript. You should read it as a string and hand it to `JSON.parse()`. (Or use a function that parses JSON from a file for you.) – Biffen Sep 03 '16 at 17:32
  • 2
    So where does `load()` come from and what does the documentation say it does? Could it be that `load()` *executes* the loaded file and not return anything? – Martijn Pieters Sep 03 '16 at 17:32
  • If `load()` executes the data, you could format the data as JSONP instead (you'd have to strip the JSONP function call from the file when loading in Python). – Martijn Pieters Sep 03 '16 at 17:33
  • @MartijnPieters you are totally right, load() does execute the code. I just tried to print something in the loaded file, it did indeed print out. Can you explain how JSONP can help me in this case? There is not much documentation to it. – return 0 Sep 03 '16 at 17:39
  • @return0: [Can anyone explain what JSONP is, in layman terms?](http://stackoverflow.com/q/3839966) – Martijn Pieters Sep 03 '16 at 17:39

1 Answers1

2

It looks like load() executes the specified file, not read it and return the contents. If this is your only option to read another file, then I suggest you use JSONP instead of JSON.

JSONP works by adding a callback around the data. Instead of:

{"key": "value"}

the file contains a function call with the data being passed in:

callback({"key": "value"});

This is meant to be executed by a JavaScript engine, causing it to execute the callback. load() would execute your file, and the callback function would be called as a result, passing in the data.

When used on the web, you'd call a JSONP service and pass in the name of the callback the service should add, but when just sharing a configuration file between a JS engine and Python, you'd hardcode that callback name.

In Python, you'd have to strip off the callback text before loading it as JSON data. That could be as easy as just removing the first N and last M characters:

with open('hardware.json', 'r') as f:
    jsonp_data = f.read()
    # remove first 9 and last 3 characters to remove JSONP callback
    data = json.loads(jsonp_data[9:-3])

A little more sophisticated technique could use newlines:

callback(
{"key": "value"}
);

to make it easier to remove the first and last line in Python. Or you could use jsonp_data.partition('(')[-1].jsonp.rpartition(')')[0] to take everything between the first ( and the last ) character in the string. Etc.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343