0

sample.json

{
"datalist": [{
        "element1": "content",
        "element2": "content",
        "element3": "content"
    }, {
        "element1": "content",
        "element2": "content",
        "element3": "content"
    }, {
        "element1": "content",
        "element2": "content",
        "element3": "content"
    }

  ]
}

Python Code

import json

file = "/home/sample.json"

with open(file) as f:
  data = json.load(f)

for elem in data["datalist"]:
  print(elem["element1"] + ' \\ ' + elem["element2"] + ' \\ ' + elem["element3"])

Good Work Print Result : element1 \ element2 \ element3

But, another sample.json

{
"datalist": [{
        "element1": "content",
        "element2": "content",
        "element3": "content"
    }, {
        "element1": "content",
        "element3": "content"
    }, {
        "element1": "content",
        "element2": "content",
        "element3": "content"
    }

  ]
}

here in second section ; element2 does not exist

if element2 does not exist, it gives KeyError: 'element2'

for elem in data["datalist"]:
  print(elem["element1"] + ' \\ ' + elem["element2"] + ' \\ ' + elem["element3"])

printing first section but when it comes second section stop and KeyError: 'element2'

how can fix it ? thx.

2
  • You can use some if statement checking the length. If the length is 2, you can print element one and element 3. Else print all three elements Commented Aug 23, 2018 at 22:08
  • thx, how to? @Bazingaa i want result : 1.line element1 \ element2 \ element3 2.line element1 \ (space or blabla) \ element3 3.line element1 \ element2 \ element3 Commented Aug 23, 2018 at 22:10

3 Answers 3

2

Python dictionaries have a get method that allows you to return a default value if the key is not found. For example, if d = { 1: 2, 3: 4 }; v = 10, d.get(1, v) gives 2, d.get(5, v) gives 10.

So this case you can just give a default value to your code such as elem.get('element2', '[element2 missing]') to signify a missing element in the print outputs and the code will run error-free.

Sign up to request clarification or add additional context in comments.

Comments

0

There are multiple options:

  • Fix the program, to tolerate missing data
  • Fix the data, to be parse-able by the program

Fixing the program

Instead of:

print(elem["element1"] + ' \\ ' + elem["element2"] + ' \\ ' + elem["element3"])

you need to check whether the elements are there:

if "element1" in elem.keys() and "element2" in elem.keys() and  "element3" in 
elem.keys():
    print(elem["element1"] + ' \\ ' ...

Fixing the data

Of course, this is easiest: add a key and a value for "element2" in the second dictionary from datalist.

2 Comments

I worked but it was incomplete and not what I wanted :( ignoring all element2 ? if there is no element2, i want to leave it space (" ") instead or write "blabla"
i want result : element1 \ element2 \ element3 element1 \ (space or blabla) \ element3 element1 \ element2 \ element3
0

The Try & Except Statement Can Be Used:

import json

file = 'home/sample.json'

with open(file) as f:
    data = json.load(f)


try:
    for elem in data["datalist"]:
        print(elem["element1"] + ' \\ ' + elem["element2"] + ' \\ ' + elem["element3"])

except KeyError as e:
    print(e, "Does Not Exist")

And There Was Some Errors In Your Code

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.