wolph / mt940

A library to parse MT940 files and returns smart Python collections for statistics and manipulation.
https://mt940.readthedocs.org/en/latest/
BSD 3-Clause "New" or "Revised" License
94 stars 50 forks source link

Simple json encoding example does not export all data properly #71

Closed MarcinOrlowski closed 5 years ago

MarcinOrlowski commented 5 years ago

I am trying to parse my bank's MT940 data with your library and example of JSON encoding is producing different results than "plain" data dump. This is what I see with plain dump:

{'amount': <-21.99 PLN>,
 'bank_reference': None,
 'currency': 'PLN',
 'customer_reference': '00000013586',
 'date': Date(2019, 3, 3),
 'entry_date': Date(2019, 3, 3),
 'extra_details': '',
 'funds_code': None,
 'guessed_entry_date': Date(2019, 3, 3),
 'id': 'S114',
 'status': 'D',
 'transaction_details': '114\n'
                        '114~00B114ZAKUP PRZY UŻYCIU KARTY\n'
                        '~20SALAD STORY SZCZECI/SZCZECI\n'
                        '~21N\n'
                        '~22                DATA TRANSA\n'
                        '~23KCJI: 2019-03-01\n'
                        '~24\n'
                        '~25\n'
                        '~26\n'
                        '~27\n'
                        '~28\n'
                        '~29\n'
                        '~30\n'
                        '~31\n'
                        '~32\n'
                        '~33\n'
                        '~34114\n'
                        '~38PL\n'
                        '~62\n'
                        '~63'}

and this is what JSON dump example outputs:

{
    "status": "D",
    "funds_code": null,
    "amount": null,
    "id": "S114",
    "customer_reference": "00000013586",
    "bank_reference": null,
    "extra_details": "",
    "currency": "PLN",
    "date": null,
    "entry_date": null,
    "guessed_entry_date": null,
    "transaction_details": "114\n114~00B114ZAKUP PRZY U\u017bYCIU KARTY\n~20SALAD STORY SZCZECI/SZCZECI\n~21N\n~22                DATA TRANSA\n~23KCJI: 2019-03-01\n~24\n~25\n~26\n~27\n~28\n~29\n~30\n~31\n~32\n~33\n~34114\n~38PL\n~62\n~63"
}

And relevant part of the source file:

:61:1903030303D21,99S11400000013586
:86:114
:86:114~00B114ZAKUP PRZY UŻYCIU KARTY
~20SALAD STORY SZCZECI/SZCZECI
~21N    
~22                DATA TRANSA
~23KCJI: 2019-03-01
~24
~25
~26
~27
~28
~29
~30
~31
~32
~33
~34114
~38PL
~62
~63
wolph commented 5 years ago

Can you show a bit of code to illustrate how you are dumping both?

While I do have some yaml loading/dumping in the code, I haven't tested with json loading/dumping so far. The yaml examples can be found here, they're used by the test system: https://github.com/WoLpH/mt940/blob/develop/tests/test_sta_parsing.py#L30-L39

My guess would be that the easiest way to get it working is by overriding JSONEncoder.default to support these types: https://docs.python.org/3/library/json.html#json.JSONEncoder.default

MarcinOrlowski commented 5 years ago

I am using examples from README. Literally as-is.

wolph commented 5 years ago

You're right. That demo is lacking quite a bit. I've updated the code in the readme to be a bit more useful :)

Does that help?

MarcinOrlowski commented 5 years ago

Thanks. No, it's still not work properly as all the dates (date, entry_date, guessed_entry_date) values are not in JSON dump.

wolph commented 5 years ago

I've created a json encoder which should handle all of the types for you. That should help a bit :)

import json
import mt940

transactions = mt940.parse('tests/jejik/abnamro.sta')

print(json.dumps(transactions, indent=4, cls=mt940.JSONEncoder))
MarcinOrlowski commented 5 years ago

That works perfect. Thank you!