objects with single key are not nested
If I use a delimiter | for example and I have a column name like lvl1|lvl2, the resulting JSON structure looks like "lvl1|lvl2" : <value>. I believe it would make more sense if the result would be "lvl1":{"lvl2":<value>}. What is the reasoning behind this? As I needed it, I've prepared the code adjustments for this and I can create a pull request for it. Thanks!
Hi there,
The default delimiters are , ,, and _. This was the default chosen in the initial version of hone and it remains as the default to preserve backwards-compatibility. If you want to specify additional delimiters like |, you can use the delimiters option like this:
Module:
import hone
optional_arguments = {
"delimiters": [" ", "_", ",", "|"] # include whatever delimiters you like
}
Hone = hone.Hone(**optional_arguments)
schema = Hone.get_schema('path/to/input.csv') # nested JSON schema for input.csv
result = Hone.convert('path/to/input.csv', schema=schema) # final structure, nested according to schema
Let me know if you have any other questions!
Hi, Thanks for super quick replies!
My issue is that if you have a csv like this (using default delimiter values):
name,ageX(years),weightX(kg),birthXday,adopted_since \n Tommy,5,3.6,11,2012
you get a json like this:
{ "adopted_since": "2012", "ageX(years)": "5", "birthXday": "11", "name": "Tommy", "weightX(kg)": "3.6" }
I believe you should get a json like this:
{ "adopted" : {"since": "2012"}, "ageX(years)": "5", "birthXday": "11", "name": "Tommy", "weightX(kg)": "3.6" }
It makes sense to me, but there might be some issues with this approach. What do you think?
Oh I see what you're saying now. The idea behind hone was that it would only add structure to your data if there was more than one column that could be nested under a key. "adopted" : {"since": "2012"} looks nice but there isn't a compelling reason to make adopted_since nested if it will only hold a single key. If there were another column like adopted_by, then the structure would make sense because you could then have "adopted" : {"since": "2012", "by": "Bob"}.
It might also get out of hand if you have multiple delimiters in a column name. For example, something like "date last accessed" would become "date" : {"last": { "accessed": ... } } if you were to split it indiscriminately. This would get cumbersome pretty quickly.
If the CSV column names are chosen well, then I can see why you might prefer to split it the way you described, but I don't think it should be the default behaviour for the reasons described above. However, if you want to add an optional argument that a user can use to make hone behave this way, then that would be totally cool with me!
If you need the feature more urgently, you can also modify the schema that hone generates to fit your needs. For example:
import hone
optional_arguments = {
"delimiters": [" ", "_", ","]
}
csv_file_path = "path/to/input.csv"
Hone = hone.Hone(**optional_arguments)
schema = Hone.get_schema(csv_file_path )
del schema["adopted_since"]
schema["adopted"] = { "since": "adopted_since" }
result = Hone.convert(csv_file_path, schema=schema)