Fix generate method to work with lists and dicts#46
Conversation
…op level JSON is a list
|
So when do you get this error exactly? Why can't you call |
|
If you use cli there is 'list' mode - see example https://github.com/bogdandm/json2python-models#f1-season-results |
One of the APIs I'm building models for returns results where the top level data structure is a list. I have a directory full of this API data that I want to be able to build a model from by running json2models -f pydantic -m ModelName /path/to/results/dir/*.json
|
Oh, I see. Maybe is it better to fix it? Should not be too hard (json_to_models/cli.py:89) Main problem with your PR is that you fix only one edge case. You do not change signature to |
This is true -- I just fixed my problem, since I was surprised that the top level data structure would cause this issue (nothing in the README about it). I'm using this module for a work project so I want it to function on all the relevant inputs. It has been very useful and a real time saver! It would be easy enough to extend the list flattening to take account of multiple levels of nesting, although if no one else has come across the issue yet, it would be a case of YAGNI... The signature of
That would mean there were two sets of I can imagine wanting to use several json2models -f pydantic \
-l Driver DriverStandings.Driver testing_tools/real_apis/f1/driver_standings.json \
-l Driver - testing_tools/real_apis/f1/drivers.jsonbut that's something I wouldn't implement unless someone actually asked for it (see: YAGNI). Let me know what you would want to make the pull request acceptable. |
|
Since your main problem is the input data format and you are using CLI, you need to fix it at CLI code. You could add logic to handle |
|
I think it might make more sense to remove the i.e. # old command: json2models -f attrs -l ModelName json.path /path/to/file.json
# new version:
json2models -f attrs --path json.path -m ModelName /path/to/file.json
# old: json2models -f attrs -l ModelName - /path/to/file.json
# new:
json2models -f attrs -m ModelName /path/to/file.json
# doesn't matter whether *.json contains a dict or a list
json2models -f attrs -m ModelName /path/to/*.json
# currently impossible
json2models -f attrs --path json.path -m ModelName /path/to/*.jsonThe |
Didn't think about it before but sounds like a good idea. Obviously it requires a lot more work than your fix (and version 0.3 release probably), few edge cases come to mind. Let do the following:
|
|
That sounds great, thank you. I may have some time to work on it myself this week or next week, so if you could push any WIP branches to the repo, I can check to make sure that I'm not duplicating your effort. |
This is a bug fix for cases where the top level data structure in a file is a list, not a dictionary.
e.g. try running json2models with
test/test_cli/data/users.jsonas the input:Result:
Flattening out the input lists in the
generatemethod allows files like this to be parsed correctly.