Skip to content
Advertisement

How To Recurisve JSON file without any loop (like:for,while,etc) in Python

I’m confuse how to create recursive function to print JSON file without any loops in Python3.

So i have home work from my college, the subject is about recursion in function, the lecturer told me to to use recursion only instead of loops.

My json file in Category.json is:

 [{'catId': 1, 'id': 1, 'name': 'Buku & Bangunan'},
 {'catId': 1, 'id': 2, 'name': 'Buku Codes & Standar'},
 {'catId': 1, 'id': 3, 'name': 'Buku Dekorasi & Ornamen'},
 {'catId': 1, 'id': 4, 'name': 'Buku Desain Dapur'},
 {'catId': 1, 'id': 5, 'name': 'Buku Desain Kamar'},
 {'catId': 1, 'id': 6, 'name': 'Buku Desain Ruang Keluarga'},
 {'catId': 1, 'id': 7, 'name': 'Buku Desain Ruang Tamu'},
 {'catId': 1, 'id': 8, 'name': 'Buku Desain Rumah'},
 {'catId': 1, 'id': 9, 'name': 'Buku Interior & Eksterior'},
 {'catId': 1, 'id': 10, 'name': 'Buku Metode & Material Bangunan'},
 {'catId': 1, 'id': 11, 'name': 'Buku Taman'},
 {'catId': 2, 'id': 12, 'name': 'Persiapan Tes CPNS'},
 {'catId': 2, 'id': 13, 'name': 'Persiapan Tes TOELF & IELTS'},
 {'catId': 2, 'id': 14, 'name': 'Persiapan TPA & Psikotest'},
 {'catId': 3, 'id': 15, 'name': 'Buku Aktivitas'},
 {'catId': 3, 'id': 16, 'name': 'Buku Cerita Anak'},
 {'catId': 3, 'id': 17, 'name': 'Buku Dongeng'},
 {'catId': 3, 'id': 18, 'name': 'Buku Dunia Pengetahuan'},
 {'catId': 3, 'id': 19, 'name': 'Buku Fabel'}]

My python code is:

import json

def showData(file):
    file_json = open(file)
    data = json.loads(file_json.read())

    return data

print(showData("Category.json"))

I’m not finishing my code because i’m still confuse how to do recursion without loops.

Do you have any idea?

Advertisement

Answer

I don’t understand why you need to use recursion just to print a JSON file instead of iterations(for, while).

This is how you print your data using Recursion.

d = [{'catId': 1, 'id': 1, 'name': 'Buku & Bangunan'},
 {'catId': 1, 'id': 2, 'name': 'Buku Codes & Standar'},
 {'catId': 1, 'id': 3, 'name': 'Buku Dekorasi & Ornamen'},
 {'catId': 1, 'id': 4, 'name': 'Buku Desain Dapur'},
 {'catId': 1, 'id': 5, 'name': 'Buku Desain Kamar'},
 {'catId': 1, 'id': 6, 'name': 'Buku Desain Ruang Keluarga'},
 {'catId': 1, 'id': 7, 'name': 'Buku Desain Ruang Tamu'},
 {'catId': 1, 'id': 8, 'name': 'Buku Desain Rumah'},
 {'catId': 1, 'id': 9, 'name': 'Buku Interior & Eksterior'},
 {'catId': 1, 'id': 10, 'name': 'Buku Metode & Material Bangunan'}]

# Recursive Code
def print_json(j):
    # Base condition
    if len(j) == 0:
        return
    # Print the first item of j
    print(j[0])
    # Call the same function on j[1:]
    # Skipping the first item of j in every call
    print_json(j[1:])
    
print_json(d)
{'catId': 1, 'id': 1, 'name': 'Buku & Bangunan'}
{'catId': 1, 'id': 2, 'name': 'Buku Codes & Standar'}
{'catId': 1, 'id': 3, 'name': 'Buku Dekorasi & Ornamen'}
{'catId': 1, 'id': 4, 'name': 'Buku Desain Dapur'}
{'catId': 1, 'id': 5, 'name': 'Buku Desain Kamar'}
{'catId': 1, 'id': 6, 'name': 'Buku Desain Ruang Keluarga'}
{'catId': 1, 'id': 7, 'name': 'Buku Desain Ruang Tamu'}
{'catId': 1, 'id': 8, 'name': 'Buku Desain Rumah'}
{'catId': 1, 'id': 9, 'name': 'Buku Interior & Eksterior'}
{'catId': 1, 'id': 10, 'name': 'Buku Metode & Material Bangunan'}

Do you know that Recursion uses something called as a Recursion Stack internally ?

That’s some extra space overhead with recursive codes.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement