0

I have this kind of JSON data and have around 100 questions. How can I filter the question only up to 40?

This is the sample of data:

data=[{"id": "AA11",
"resp": [
    {"answer": "A","number": 1},
    {"answer": "A","number": 2},
    {"answer": "B","number": 3},
    {"answer": "D","number": 4},
    {"answer": "E","number": 5}
]},
{"id": "AA22",
"resp": [
    {"answer": "A","number": 1},
    {"answer": "A","number": 2},
    {"answer": "B","number": 3},
    {"answer": "D","number": 4},
    {"answer": "E","number": 5}
]},
{"id": "AA33",
"resp": [
    {"answer": "A","number": 1},
    {"answer": "A","number": 2},
    {"answer": "B","number": 3},
    {"answer": "D","number": 4},
    {"answer": "E","number": 5}
]}]

Can I extract the data only up to number 3 as example in this case?

Output:

data=[{"id": "AA11",
"resp": [
    {"answer": "A","number": 1},
    {"answer": "A","number": 2},
    {"answer": "B","number": 3}
]},
{"id": "AA22",
"resp": [
    {"answer": "A","number": 1},
    {"answer": "A","number": 2},
    {"answer": "B","number": 3}
]},
{"id": "AA33",
"resp": [
    {"answer": "A","number": 1},
    {"answer": "A","number": 2},
    {"answer": "B","number": 3}
]}]
1

4 Answers 4

2
for item in data:
    item['resp'] = item['resp'][:3]
Sign up to request clarification or add additional context in comments.

Comments

2

The Pythonic way would be a comprehension:

filtered = [{'id': d['id'], 'resp': [x for x in d['resp'] if x['number'] <= 3]}
            for d in data]

It gives as expected:

[{'id': 'AA11',
  'resp': [{'answer': 'A', 'number': 1},
           {'answer': 'A', 'number': 2},
           {'answer': 'B', 'number': 3}]},
 {'id': 'AA22',
  'resp': [{'answer': 'A', 'number': 1},
           {'answer': 'A', 'number': 2},
           {'answer': 'B', 'number': 3}]},
 {'id': 'AA33',
  'resp': [{'answer': 'A', 'number': 1},
           {'answer': 'A', 'number': 2},
           {'answer': 'B', 'number': 3}]}]

1 Comment

"...Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts..." -The Zen of Python
1

Filtering up to a specific element should can be done like this

def filter(question,num):
    temp = {}
    for key in question:
        temp[key] = question[key]
    temp['resp'] = question['resp'][:num]
    return temp

def filter_list(questions,num):
    temp = []
    for question in questions:
        temp.append(filter(question,num))
    return temp

data = [{}...]
filtered_data = filter_list(data)


You'll notice that filter creates a copy of question and only changes the copy. It can be useful to change data structures, but it's good practice to treat data structures as immutable where possible.

As a bonus, you won't lose any of the original data this way

Comments

1

Try this :

new_data = data[0:40]
for item in new_data:
    item['resp'] = item['resp'][0:3]

This solution will create a new dictionary with expected values.

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.