Skip to content
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

Ability to output empty objects as null instead of {} #179

Open
OSalama opened this issue Sep 2, 2024 · 1 comment
Open

Ability to output empty objects as null instead of {} #179

OSalama opened this issue Sep 2, 2024 · 1 comment

Comments

@OSalama
Copy link

OSalama commented Sep 2, 2024

Is your feature request related to a problem? Please describe.
I'm loading data output by yyjson into Spark/Hive tables, and the fact that empty objects are output as {} instead of null is causing a lot of issues, because Spark is not able to infer any meaningful schema and ends up dropping the empty object fields.

Describe the solution you'd like
Output empty objects as null instead of {}. I'd be happy to have to use a flag to achieve this.

Describe alternatives you've considered
The only alternatives I can see would be outside the scope of yyjson, eg:

  1. post-processing to convert all {} to null - this feels like it would be really slow and defeat a lot of the point of using yyjson.
  2. upstream code changes to ensure we define a schema for our empty dictionaries, so {} becomes {optional_field_1: null, optional_field_2:null, ...} - this feels "wrong", since there is a distinction between a null object and an object with keys and null values.
  3. define a full schema for our Spark tables. This will work, but it's not how I'd like to do things on this particular project for other reasons.

Additional context
N/A

@ibireme
Copy link
Owner

ibireme commented Sep 3, 2024

It seems this requirement might not be very common, could you provide more details about how you're using yyjson to output JSON?

If you're building JSON using yyjson_mut_val, I suggest adding a single line of code after you finish creating an object:

yyjson_mut_val *obj = ...
if (!yyjson_mut_obj_size(obj)) yyjson_mut_set_null(obj);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants