Skip to content

#827 Add implementation for the new JSON connector - #835

Draft
lmolotii wants to merge 8 commits into
mainfrom
json-connector
Draft

#827 Add implementation for the new JSON connector#835
lmolotii wants to merge 8 commits into
mainfrom
json-connector

Conversation

@lmolotii

@lmolotii lmolotii commented Nov 26, 2025

Copy link
Copy Markdown
Contributor

JSON Connector specification

Overview

Implements the basic JSON connector that supports reading & writing JSON and JSONL (JSON Lines) files, it extends the existing implementation of the file connector, providing more options to configure the connector.

Usage Examples

Reading JSON Files

import wrangles

# Basic JSON read
df = wrangles.connectors.json.read('data.json')

# With column selection
df = wrangles.connectors.json.read('data.json', columns=['col1', 'col2'])

# With specific orientation
df = wrangles.connectors.json.read('data.json', orient='records')

# With encoding
df = wrangles.connectors.json.read('data.json', encoding='utf-8')

Reading JSONL Files

# Basic JSONL read
df = wrangles.connectors.json.read('data.jsonl')

# With nrows (only works for JSONL!)
df = wrangles.connectors.json.read('data.jsonl', nrows=100)

# Explicit lines parameter
df = wrangles.connectors.json.read('data.jsonl', lines=True, nrows=50)

Using in Recipes

# Read JSON
read:
  - json:
      name: input.json
      orient: records

# Read JSONL with row limit
read:
  - json:
      name: input.jsonl
      nrows: 1000

# Write JSON with pretty printing
write:
  - json:
      name: output.json
      indent: 2

# Write JSONL
write:
  - json:
      name: output.jsonl

Writing JSON/JSONL Files

import pandas as pd
import wrangles

df = pd.DataFrame({'col1': [1, 2, 3], 'col2': ['a', 'b', 'c']})

# Write JSON with indentation
wrangles.connectors.json.write(df, 'output.json', indent=2)

# Write JSONL
wrangles.connectors.json.write(df, 'output.jsonl')

# Write with column selection
wrangles.connectors.json.write(df, 'output.json', columns=['col1'])

# Write with specific orientation
wrangles.connectors.json.write(df, 'output.json', orient='records')

Explicit Connector Parameters

All parameters are explicitly documented and validated:

Read Parameters:

  • name - File path (required)
  • columns - Subset of columns to read
  • orient - JSON format ('split', 'records', 'index', 'columns', 'values')
  • encoding - File encoding (default: 'utf-8')
  • nrows - Number of rows to read (JSONL only!)
  • lines - Whether file is line-delimited (auto-detected)
  • compression - Compression type ('infer', 'gzip', 'bz2', 'zip', 'xz')

Write Parameters:

  • name - File path (required)
  • columns - Subset of columns to write
  • orient - JSON format
  • indent - Indentation level for pretty-printing (JSON only, not JSONL)
  • lines - Write as line-delimited (auto-detected)
  • compression - Compression type

@lmolotii lmolotii added this to the v1.16 milestone Nov 26, 2025

@ebhills ebhills left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

In you test examples your recipe looks like:

read:
  json: ...

read / wrangles / write objects contain lists so each read / write must be a list element like:

read:
  - json:

@lmolotii

Copy link
Copy Markdown
Contributor Author

@ebhills, yes, the example description is not correct. I've fixed it. However, the implementation is ok. Please check for the 'test_write_json_basic' unit test to verify.

@lmolotii lmolotii self-assigned this Nov 27, 2025
@lmolotii
lmolotii requested a review from ebhills December 2, 2025 21:12
VLysakBinariks
VLysakBinariks previously approved these changes Dec 4, 2025
@lmolotii
lmolotii dismissed ebhills’s stale review December 4, 2025 13:17

@ebhills, yes, the example description is not correct. I've fixed it. However, the implementation is ok. Please check for the 'test_write_json_basic' unit test to verify.

@ChrisWRWX ChrisWRWX linked an issue Dec 5, 2025 that may be closed by this pull request
@ebhills

ebhills commented Dec 12, 2025

Copy link
Copy Markdown
Collaborator

@lmolotii - the test syntax is still wrong. Even if it works, I would prefer to document / test using the correct syntax.

@lmolotii

Copy link
Copy Markdown
Contributor Author

@lmolotii - the test syntax is still wrong. Even if it works, I would prefer to document / test using the correct syntax.

I've adjusted the tests to be compliant with the syntax remarks. Please check.

@thomasstvr

Copy link
Copy Markdown
Collaborator

@lmolotii we should add tests with multiple parameters used at the same time.

Also, the massive amount of if statements seems a bit hacky. Would be nice to condense those in some way.

@lmolotii

Copy link
Copy Markdown
Contributor Author

I've added multiple tests to cover multi-parameter scenarios. However, I don't think that we can do something about multiple if's. Basically, there, we try to configure the pandas_kwargs, and if an argument is present, then we set the config in kwargs. How to avoid it, without adding the complexity for the implementation, is a good question.

I had several options in mind:

  1. Build the dict in one statement and filter out default values
  2. Parameterized mapping approach - to define a configuration that maps parameters to their defaults, then iterate
  3. Helper function approach -where we can create a reusable utility to handle conditional kwargs building

But, it is just adds more code, without adding any value... I would prefer to keep it simple and in one place.

@lmolotii

Copy link
Copy Markdown
Contributor Author

I've updated the initialization of the pandas_kwargs. Please review it and let me know if you are ok with this approach.

@lmolotii lmolotii changed the title #827 Add implementation for the new JSON connector, add corresponding tests #827 Add implementation for the new JSON connector Dec 26, 2025
date_unit: str = None,
encoding: str = None,
encoding_errors: str = 'strict',
lines: bool = None,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Users should not be allowed to set this parameter. This just opens the door for something like below, which there is no error handling for.

read:
  - json:
      name: tests/samples/data.json
      lines: True

pandas_kwargs['orient'] = orient
else:
pandas_kwargs['orient'] = 'records'

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Instead of setting so many parameters to None, lets just set them to the default in the first place. This will clean up all of the if statements, and also be much easier to quickly tell what the default is set to.

"""
Test writing a basic .json file
"""
filename = f"tests/temp/{_uuid.uuid4()}.json"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The file connector uses a small handful of the same files to read and write, let's mimic that here. It just makes it easier to actually check the file being read/written if need be. For an example, see test_write_jsonl in test_file.py.

df = df[columns]


path_matched = _re.search(r'^.+(?=\/\w+\.\w+)', name)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please add comments where any ambiguity might exist, like here.

@thomasstvr thomasstvr modified the milestones: v1.16, v1.17 Jan 22, 2026
@thomasstvr thomasstvr modified the milestones: v1.17, v1.18 Mar 31, 2026
@ebhills
ebhills marked this pull request as draft July 27, 2026 14:02
@ebhills
ebhills removed their request for review July 27, 2026 14:04

ebhills commented Jul 27, 2026

Copy link
Copy Markdown
Collaborator

Queue triage (2026-07-27)

  • Disposition: Draft — delivery-owner action
  • Delivery owner: @lmolotii
  • Next action: Decide whether issue new connector: JSON #827 is still wanted. If yes, update from main, resolve the merge conflict and four open review threads, and narrow the 1,379-line change before requesting a reviewer.

GitHub is the status record; update this PR rather than the external spreadsheet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

new connector: JSON

4 participants