Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Validate JSON file
#11
exactly, there is just a function definition, you never call it. also, the text argument is not used. better, something within these lines:

import json
    
def parse(fname):
    try:
        with open(fname) as f:
            return json.load(f)
    except ValueError as e:
        print('invalid json: %s' % e)
        return None
file_name = r"C:\python-script\file\assetLink_tr-TR.json"
print(parse(file_name))
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
#12
Thanks, that works now. However, if the JSON file is saved as UTF-8 encoding, it will not read the file, the scripts throws up the following message

invalid json: 'charmap' codec can't decode byte 0x81 in position 145717: character maps to <undefined>

I also discovered that if I enter (python -m json.tool "C:\Engineering\test2\assetLink_tr-TR.json") on the CMD line, it works as what I am looking for. But I would still like to find out why the script does not work on UTF-8 JSON files.

Thanks a lot for all the help
Bella
Reply
#13
(Feb-27-2020, 08:41 AM)BellaMac Wrote: invalid json: 'charmap' codec can't decode byte 0x81 in position 145717: character maps to <undefined>

I also discovered that if I enter (python -m json.tool "C:\Engineering\test2\assetLink_tr-TR.json") on the CMD line, it works as what I am looking for. But I would still like to find out why the script does not work on UTF-8 JSON files.
Remember on Widows should and most in many cases most specify which encoding that should be used.
If not it will use charmap codec.
with open(fname, encoding='utf-8') as f:
The default encoding for Python 3 source code is utf-8,but read in and out OS may mess that up.
So as example both read and write do specify what encoding to use,
and that's always utf-8 if code starts in Python or as a first try in almost all cases.
s = 'Crème and Spicy jalapeño ☂'
with open('unicode.txt', 'w', encoding='utf-8') as f_out:
    f_out.write(s)

with open('unicode.txt', encoding='utf-8') as f:
    print(f.read())
Output:
Crème and Spicy jalapeño ☂
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  parse json field from csv file lebossejames 4 668 Nov-14-2023, 11:34 PM
Last Post: snippsat
  Python Script to convert Json to CSV file chvsnarayana 8 2,343 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Loop through json file and reset values [SOLVED] AlphaInc 2 1,960 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,164 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  validate large json file with millions of records in batches herobpv 3 1,221 Dec-10-2022, 10:36 PM
Last Post: bowlofred
  Create SQL connection function and validate mg24 1 905 Sep-30-2022, 07:45 PM
Last Post: deanhystad
Sad how to validate user input from database johnconar 3 1,836 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,337 Jul-16-2022, 02:05 PM
Last Post: deanhystad
  Initializing, reading and updating a large JSON file medatib531 0 1,723 Mar-10-2022, 07:58 PM
Last Post: medatib531

Forum Jump:

User Panel Messages

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