How to access the same JSON file from both javascript and python?
<p>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.</p>

<p>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.</p>

<pre><code>// 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.
</code></pre>

<p>Already failed by this step because '<code>hardware.json</code>' is not using javascript syntax...</p>

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

<pre><code>{
    "hardware1": {
        "ID": "xxx"
    },
    "hardware2": {
        "ID": "yyy"
    }
}
</code></pre>

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

<pre><code>with open('hardware.json', 'r') as f:
    data = json.load(f)
</code></pre>

<p>It looks like <code>load()</code> <em>executes</em> the specified file, not read it and return the contents. If this is your <em>only</em> option to read another file, then I suggest you use <a href="https://en.wikipedia.org/wiki/JSONP" rel="nofollow">JSONP</a> instead of JSON.</p>

<p>JSONP works by adding a <em>callback</em> around the data. Instead of:</p>

<pre><code>{"key": "value"}
</code></pre>

<p>the file contains a function call with the data being passed in:</p>

<pre><code>callback({"key": "value"});
</code></pre>

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

<p>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.</p>

<p>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:</p>

<pre><code>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])
</code></pre>

<p>A little more sophisticated technique could use newlines:</p>

<pre><code>callback(
{"key": "value"}
);
</code></pre>

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

javascript python json