Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Validate JSON file
#1
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
Reply
#2
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
Reply
#3
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
Reply
#4
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__
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
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
Reply
#6
Can anyone help me with the code.

Much appreciated.

Bella
Reply
#7
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#8
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
Reply
#9
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  encrypt data in json file help jacksfrustration 1 65 Yesterday, 05:16 PM
Last Post: deanhystad
  parse json field from csv file lebossejames 4 669 Nov-14-2023, 11:34 PM
Last Post: snippsat
  Python Script to convert Json to CSV file chvsnarayana 8 2,346 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Loop through json file and reset values [SOLVED] AlphaInc 2 1,962 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,173 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  validate large json file with millions of records in batches herobpv 3 1,222 Dec-10-2022, 10:36 PM
Last Post: bowlofred
  Create SQL connection function and validate mg24 1 906 Sep-30-2022, 07:45 PM
Last Post: deanhystad
Sad how to validate user input from database johnconar 3 1,837 Sep-11-2022, 12:36 PM
Last Post: ndc85430
  Writing to json file ebolisa 1 970 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Trying to parse only 3 key values from json file cubangt 8 3,338 Jul-16-2022, 02:05 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020