Skip to content

BUG: fix to_json() with JSON Table Schema work correctly with string dtype #61900

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 26, 2025

Conversation

khemkaran10
Copy link
Contributor

Fixes: #61889

To ensure consistent behavior for to_json(), when dtype="str" is used, it will now output "type": "string" instead of "type": "any".

Before Fix:

>>> pd.Series(["a", "b", None], dtype="str").to_json(orient="table", index=False)
'{"schema":{"fields":[{"name":"values","type":"any","extDtype":"str"}],"pandas_version":"1.4.0"},"data":[{"values":"a"},{"values":"b"},{"values":null}]}'

After Fix:

>>> pd.Series(["a", "b", None], dtype="str").to_json(orient="table", index=False)
'{"schema":{"fields":[{"name":"values","type":"string","extDtype":"str"}],"pandas_version":"1.4.0"},"data":[{"values":"a"},{"values":"b"},{"values":null}]}'

@mroeschke mroeschke added IO JSON read_json, to_json, json_normalize Strings String extension data type and string data labels Jul 21, 2025
@jorisvandenbossche jorisvandenbossche added this to the 2.3.2 milestone Jul 26, 2025
Copy link
Member

@jorisvandenbossche jorisvandenbossche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! (just added a brief whatsnew note)

@jorisvandenbossche jorisvandenbossche changed the title BUG FIX: to_json() with JSON Table Schema work correctly with string dtype. BUG: fix to_json() with JSON Table Schema work correctly with string dtype Jul 26, 2025
@jorisvandenbossche jorisvandenbossche merged commit a067fff into pandas-dev:main Jul 26, 2025
47 checks passed
Copy link

lumberbot-app bot commented Jul 26, 2025

Owee, I'm MrMeeseeks, Look at me.

There seem to be a conflict, please backport manually. Here are approximate instructions:

  1. Checkout backport branch and update it.
git checkout 2.3.x
git pull
  1. Cherry pick the first parent branch of the this PR on top of the older branch:
git cherry-pick -x -m1 a067fff92540118cd784349b6e79021087347768
  1. You will likely have some merge/cherry-pick conflict here, fix them and commit:
git commit -am 'Backport PR #61900: BUG: fix to_json() with JSON Table Schema work correctly with string dtype'
  1. Push to a named branch:
git push YOURFORK 2.3.x:auto-backport-of-pr-61900-on-2.3.x
  1. Create a PR against branch 2.3.x, I would have named this PR:

"Backport PR #61900 on branch 2.3.x (BUG: fix to_json() with JSON Table Schema work correctly with string dtype)"

And apply the correct labels and milestones.

Congratulations — you did some good work! Hopefully your backport PR will be tested by the continuous integration and merged soon!

Remember to remove the Still Needs Manual Backport label once the PR gets merged.

If these instructions are inaccurate, feel free to suggest an improvement.

@jorisvandenbossche
Copy link
Member

Thanks @khemkaran10!

jorisvandenbossche added a commit to jorisvandenbossche/pandas that referenced this pull request Jul 26, 2025
mroeschke pushed a commit that referenced this pull request Jul 26, 2025
…ctly with string dtype (#61900) (#61965)

Co-authored-by: Khemkaran Sevta <[email protected]>
Co-authored-by: Khemkaran <[email protected]>
@jorisvandenbossche
Copy link
Member

Manual backport -> #61965

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO JSON read_json, to_json, json_normalize Strings String extension data type and string data
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: make to_json with JSON Table Schema work correctly with string dtype
3 participants