0

I am trying to write a Python function as part of my job to be able to check the existence of data in a JSON file which I can only get by downloading it from a website. I am the only resource here with any coding or scripting experience (HTML, CSS & SQL) so this has fallen to me to sort out. I have no experience thus far with Python.

I am not allowed to change the structure or format of the JSON file, the format of it is:

{
    "naglowek": {
        "dataGenerowaniaDanych": "20210514",
        "liczbaTransformacji": "5000",
        "schemat": "RRRRMMDDNNNNNNNNNNBBBBBBBBBBBBBBBBBBBBBBBBBB"
    },
    "skrotyPodatnikowCzynnych": [
        "examplestring1",
        "examplestring2",
        "examplestring3",
        "examplestring4",
    ],
    "maski": [
        "examplemask1",
        "examplemask2",
        "examplemask3",
        "examplemask4"
    ]
}

I have tried numerous examples found online but none of them seem to work. From looking at various websites the Python code I have is:

import json

with open('20210514.json') as myfile:
    data = json.load(myfile)

print(data)

keyVal = 'examplestring2'

if keyVal in data:
    # Print the success message and the value of the key
    print("Data is found in JSON data")
else:
    # Print the message if the value does not exist
    print("Data is not found in JSON data")

But I am getting these errors below, I am a complete newbie to Python so am having trouble deciphering them:

D:\PycharmProjects\venv\Scripts\python.exe D:/PycharmProjects/json_test.py
Traceback (most recent call last):
  File "D:\PycharmProjects\json_test.py", line 4, in <module>
    data = json.load(myfile)
  File "C:\Users\xyz\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 293, in load
    return loads(fp.read(),
  File "C:\Users\xyz\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "C:\Users\xyz\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\xyz\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 12 column 5 (char 921)

Process finished with exit code 1

Any help would be massively appreciated!

1
  • 3
    Looks like it's not happy with the trailing comma after "examplestring4". If you remove that, you'll probably have more luck. Commented May 17, 2021 at 20:22

3 Answers 3

2
{
    "naglowek": {
        "dataGenerowaniaDanych": "20210514",
        "liczbaTransformacji": "5000",
        "schemat": "RRRRMMDDNNNNNNNNNNBBBBBBBBBBBBBBBBBBBBBBBBBB"
    },
    "skrotyPodatnikowCzynnych": [
        "examplestring1",
        "examplestring2",
        "examplestring3",
        "examplestring4"
    ],
    "maski": [
        "examplemask1",
        "examplemask2",
        "examplemask3",
        "examplemask4"
    ]
}

This should work. The problem here is that you have a comma at the end of a list which your parser can't handle. ECMAScript 5 introduced the ability to parse that. But apparently JSON in general doesn't support it (yet?). So, make sure to not have a comma at the end of a list.

For your if-else statement to be correct, you'd have to change it to something like this:

keyVal = 'examplestring2'
keyName = 'skrotyPodatnikowCzynnych'

if keyName in data.keys() and keyval in data[keyName]:
  # Print the success message and the value of the key
  print("Data is found in JSON data")
  
else:
  # Print the message if the value does not exist
  print("Data is not found in JSON data")
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the answers so far. Removing the comma has got me one step closer but even though the string I'm looking up is there, it always returns the ELSE part of the statement to say the value cannot be found.
It only works if I'm looking for the key "skrotyPodatnikowCzynnych", not the values within that key.
Well, it's obvious that you're not comparing a string to a string but rather a dictionary to a string. I will edit my answer to let it reflect what I mean.
0

Remove the trailing comma. JSON specification does not allow a trailing comma

Comments

0

If you don't want to change the file structure then you have to do this:

import yaml

with open('20210514.json') as myfile:
    data = yaml.load(myfile, Loader=yaml.FullLoader)

print(data)

You also need to install yaml first. https://pyyaml.org/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.