Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

import json 

import requests 

import sys 

 

 

def readfromjson(filename: str) -> dict: 

    """ 

    Reads a json string and emits json string 

    """ 

    try: 

        json_data = open(filename) 

        data = json.load(json_data) 

        json_data.close() 

        print(type(data)) 

        return data 

    except ValueError as e: 

        print(e) 

        sys.exit(e) 

    except IOError as e: 

        print(e) 

        sys.exit(e) 

 

 

def readfromurl(url: str, params: dict = None) -> dict: 

    """ 

    Loads json from an URL over the internets 

    """ 

    response = requests.get(url, params=params) 

    if response.status_code == 200: 

        data = response.json() 

        return data 

    else: 

        sys.exit(response.text) 

 

 

def readfromstring(jsondata: str) -> dict: 

    """ 

    Loads json from string 

    """ 

    if type(jsondata) is not str: 

        sys.exit("the input doesn't seem to be valid string") 

    try: 

        data = json.loads(jsondata) 

    except ValueError as e: 

        print(e) 

        sys.exit(e) 

    except Exception as e: 

        print(e) 

        sys.exit(e) 

    return data