top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to verify JSON Data in Python?

0 votes
848 views

Would someone let me know how to verify JSON data in python. There are so many modules available to verify XML file, however I didn't find any good module to verify JSON Data.

After searching on the internet I came across JSON module, however it only coverts the JSON data to python. it's good, however the problem comes when JSON response is very large.

Is there any module through which I can verify JSON file like DOM or Object oriented way. ( i.e. data.key)

posted May 26, 2014 by Amit Parthsarthi

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

Python comes with a built-in json module. Just use json.load() or json.loads() to parse your JSON data. The first call reads from a string, the second on from a file, but in all other ways, they're identical.

There are a bunch of third-party modules (ujson, etc) which are faster, but fundamentally, they're all the same.

If I understand you correctly, you're reading a JSON document which is so large that if you store the converted data as a Python object, you run out of memory? If that's the case, I'm not sure if there's a good pure Python solution. I don't know of any json modules which parse, but don't store, the data.

Depending on what operating system you're on, there may be a command-line utility which parse JSON. For example, on Ubuntu linux, there's "json_xs". Perhaps shell out to that, use the "-t null" output format, redirect the output to /dev/null, and see what exit status you get:

# Good JSON
$ echo '[1, 2, 3]' | json_xs -t null 2>/dev/null; echo $?
0

# Bad JSON
$ echo '[1; 2, 3]' | json_xs -t null 2>/dev/null; echo $?
255

Wrap this up in a subprocess.check_output() call, and you're done.

answer May 26, 2014 by anonymous
Similar Questions
+1 vote

How can you detect if a key is duplicated in a JSON file? Example:

{
 "something": [...],
 ...
 "something": [...]
}

I have a growing JSON file that I edit manually and it might happen that I repeat a key. If this happens, I would like to get notified. Currently the value of the second key silently overwrites the value of the first.

Do you know about a command line JSON validator?

0 votes

i want to sent a response for my form submit from server to client, that means python flask to javascript. My javascript code is given follows

document.addEventListener('DOMContentLoaded', function() {

chrome.tabs.getSelected(null, function(tab) {
  d = document;
  var f = d.createElement('form');
  f.action = 'http://127.0.0.1:5000/Get%20Form/';
  f.method = 'post';
  var i = d.createElement('input');
  i.type = 'hidden';
  i.name = 'url';
  i.value = tab.url;
  f.appendChild(i);
  d.body.appendChild(f);
  f.submit();   
});
$(".button").click(function(){
    request = new XMLHttpRequest();
    request.open("POST","http://127.0.0.1:5000/PutValue/",true);
    request.send();
    request.addEventListener("readystatechange", processRequest,false);
    function processRequest(e)
    {
    if(request.readyState==4 && request.status == 200)
    {
    var response = JSON.parse(request.responseText);
    a=response.result
    alert(a);
    }
    }
});
},false);

And my Python server code is follows

from flask import Flask, flash, redirect, url_for, request, render_template,jsonify
import json
import UrlTest
import trainingSet as ts

app = Flask(__name__)
user=""
s=0

@app.route('/Get Form/',methods = ['POST'])
def GetForm():
request.method == 'POST'
url=request.form['url']
UrlTest.process_test_url(url,'test_features.csv')
s=ts.main_caller('url_features.csv','test_features.csv')
print s
return str(s)

@app.route('/PutValue/',methods = ['POST'])
def PutValue():
request.method == 'POST'
print s
return jsonify(result=s)


if (__name__ == '__main__'):
app.run(debug=True,host='0.0.0.0', use_reloader=False)

I want to send the value of s to the javascript client. please help me to send this the value of s.
and if u can suggest the complete code in javascipt and python

+1 vote

Hi i need to create a bar chart with using json data.I saw many examples for loading csv datas.But i cant able to find for loading json datas.Please anyone provide example for this.

0 votes

Here i am struggling to fetch json data from web service in SWIFT. So, i am looking for some idea to do this.

...