Skip to content

Commit

Permalink
docs: Document that "/" in JSON keys can cause collisions, closes #767
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed Oct 18, 2023
1 parent 9331192 commit 71211ad
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
19 changes: 19 additions & 0 deletions agate/table/from_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ def from_object(cls, obj, row_names=None, column_types=None):
Not all rows are required to have the same keys. Missing elements will
be filled in with null values.
Keys containing a slash (``/``) can collide with other keys. For example:
.. code-block:: python
{
'a/b': 2,
'a': {
'b': False
}
}
Would generate:
.. code-block:: python
{
'a/b': false
}
:param obj:
Filepath or file-like object from which to read JSON data.
:param row_names:
Expand Down
8 changes: 8 additions & 0 deletions examples/test_from_json_ambiguous.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"a/b": 2,
"a": {
"b": false
}
}
]
8 changes: 8 additions & 0 deletions tests/test_from_json.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from agate import Table
from agate.data_types import Boolean, Date, DateTime, Number, Text, TimeDelta
from agate.rows import Row
from agate.testcase import AgateTestCase
from agate.type_tester import TypeTester

Expand Down Expand Up @@ -85,3 +86,10 @@ def test_from_json_no_type_tester(self):
def test_from_json_error_newline_key(self):
with self.assertRaises(ValueError):
Table.from_json('examples/test.json', newline=True, key='test')

def test_from_json_ambiguous(self):
table = Table.from_json('examples/test_from_json_ambiguous.json')

self.assertColumnNames(table, ('a/b',))
self.assertColumnTypes(table, [Boolean])
self.assertRows(table, [Row([False])])

0 comments on commit 71211ad

Please sign in to comment.