Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
912 B
2
Indexable
Never
from dataclasses import dataclass
import json
import csv
from dataclasses import dataclass
import pandas as pd
# from dataclasses_json import dataclass_json

JSON_SCHEMA = {'column_a': 'none',
               'column_b': 'none',
               'column_c': 'none'}


class JsonParser:
    def export(self, serialized_json, filename):
        df = pd.read_json(serialized_json)
        df.to_csv(filename)

    # receive either json string or json file.
    # this implementation receives json string
    def parse_json(self, json_string):
        copied = JSON_SCHEMA
        dict_from_string = json.loads(json_string)
        for key in JSON_SCHEMA.keys():
            if key in dict_from_string.keys():
                copied[key] = dict_from_string.get(key)
        return copied


parser = JsonParser()
ser = parser.parse_json('{"column_a": "asasd", "column_c":"asdasd"}')
parser.export(ser, 'foo')
print(ser)