Python Forum

Full Version: Validate JSON file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hi, I am new to the python language (well new to any programming language). From what I have been reding on-line, python has components to validate JSON files, which is what I need to do. In stalled Python 3.81 and found a script on-line that says it will validate a JSON file, however, It is not working for me as I think I am not using it correctly

Here is the code in the script
import json

def parse(text):
    try:
    with open("C:\python-script\file\assetLink_tr-TR.json") as f:
    return json.load(f)
    except ValueError as e:
        print('invalid json: %s' % e)
        return None # or: raise
Here is the error i get when i run the script on the CMD

File "C:\python-script\JSON_Validate.py", line 5
with open("C:\python-script\file\assetLink_tr-TR.json") as f:
^
IndentationError: expected an indented block

I appreciate any help

Bella
You need to indent the line starting "with"

try:
    with open("C:\python-script\file\assetLink_tr-TR.json") as f:
    return json.load(f)
Regards
Thanks for the reply, but now getting the error for the "return json.load(f)" line of code.

This is what i changed the code to

import json
 
def parse(text):
    try:
        with open("C:\python-script\file\assetLink_tr-TR.json") as f:
        return json.load(f)
    except ValueError as e:
        print('invalid json: %s' % e)
        return None # or: raise
This is the new error
File "C:\python-script\JSON_Validate.py", line 6
return json.load(f)
^
IndentationError: expected an indented block

if the indentation is still wrong, can you please indent the 9 lines so i see where i am going wrong.

Once again, thanks for your help
import json
  
def parse(text):
    try:
        with open("C:\python-script\file\assetLink_tr-TR.json") as f:
            return json.load(f)
    except ValueError as e:
        print('invalid json: %s' % e)
        return None
All statements with a colon at the end, introduce a block.
You've to indent the block.


def foo():
    ...

with open("file") as fd:
    ...


if False:
    ...
elif False:
    ...
else:
    ...


while True:
    ...


for i in range(10):
    ...


class:
    ...  # code block of class
    # one method in the code block
    def __init__(self):
        ...  # code block of method __init__
Thanks, for the help with the Indenting.

However, the validation does not work as expected. When i run the script, it does not throw up an exception regarding issues in the JSON file as i know there are issues in the file as i tested the file on an online JSON validation tool.

Do i need to add more to the code so to the script will list the issues.

Thanks
Can anyone help me with the code.

Much appreciated.

Bella
First you wanted to catch the Exception and now you don't want to catch it?
If a ValueError occurs, the function return None.
If no ValueError happens, then you get the result.
Hi, I do want to catch the exception (I apologise if I have confused you), I want the script to tell me what line has the issue as what occurs when validating online.

But when I run the script using the CMD line, nothing happens, nothing gets displayed.

Thanks
(Feb-26-2020, 05:46 PM)BellaMac Wrote: [ -> ]nothing happens, nothing gets displayed.
show your code in python tags. What DeaD_EyE show you is a function, but in his snippet the function is never called
This is the code in the script as what I posted previous

import json
   
def parse(text):
    try:
        with open("C:\python-script\file\assetLink_tr-TR.json") as f:
            return json.load(f)
    except ValueError as e:
        print('invalid json: %s' % e)
        return None
Thanks
Pages: 1 2