Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I send a JSON object from a Python script to jQuery?

I've looked through APIs and all sorts of resources, but I can't seem to get the hang of fetching a JSON object from a Python script using AJAX. I'm sure the issue is with how I'm dealing with the JSON object.

First, in a python script on my server, I generate and print a JSON array

import json
print "Content-type: application/json"
print 
print json.dumps(['Price',{'Cost':'99'}])

Then, in a separate html file, I try something like

<body>
<div id="test">
</div>

<script>
 $(document).ready(function() {
  $.getJSON("http://www.example.com/cgi-bin/makeJSON.py", function(data) {
        $('#test').html("JSON Data: " + data.Price);
    });
});
</script>
</body>

But I don't get anything. I'm sure that data.Price is wrong, but I'm also pretty certain that I should be doing something instead of just printing the results of json.dumps

Any help is appreciated! Thanks in advance, and sorry if this is an obvious question.

like image 931
Parker Avatar asked Nov 30 '10 16:11

Parker


People also ask

How do I export JSON data in Python?

Method 1: Writing JSON to a file in Python using json. dumps() The JSON package in Python has a function called json. dumps() that helps in converting a dictionary to a JSON object.

How JSON file is used in jQuery?

To load JSON data using jQuery, use the getJSON() and ajax() method. The jQuery. getJSON( ) method loads JSON data from the server using a GET HTTP request. data − This optional parameter represents key/value pairs that will be sent to the server.

Can I load JSON from a string Python?

Use the json.The json. loads() function accepts as input a valid string and converts it to a Python dictionary. This process is called deserialization – the act of converting a string to an object.

How do I parse a JSON in Python?

If you need to parse a JSON string that returns a dictionary, then you can use the json. loads() method. If you need to parse a JSON file that returns a dictionary, then you can use the json. load() method.


2 Answers

In your case you have enclosed the JSON response in an array. To access price you need to access data[0]. You need to structure your JSON data properly.

The following changes in your Python script should allow you to access data.Price. Let me know in case you still face any issues.

   import json
   print "Content-type: application/json"
   print 
   response={'Price':54,'Cost':'99'}
   print(json.JSONEncoder().encode(response))
like image 96
Philar Avatar answered Nov 14 '22 18:11

Philar


Please provide more context to what you're trying to do. Can you just hard-code the JSON object into the HTML? Or, are you trying to do something more dynamic, like AJAX?

For the former, view the HTML source of the page that Python generates. It must look something like:

<script type="text/javascript">
   var MY_GLOBAL_JSON_OBJECT = { ... };
</script>

Or you could just spit out the JSON object inside the function that's actually using it instead of assigning it first to a global variable.

like image 42
ma11hew28 Avatar answered Nov 14 '22 17:11

ma11hew28