-
Notifications
You must be signed in to change notification settings - Fork 137
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
Flatten and Unflatten Complex JSON #228
Comments
seems the source of the data coming from Parquet file. Can you please share the schema with sample data? |
I will explain this. {
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "some.schema.json",
"title": "desserts",
"type": "object",
"additionalProperties": false,
"required": [
"batters"
],
"properties": {
"id": {
"description": "System generated unique identifier for category. (A UUID specified by RFC4122).",
"type": "string",
"format": "uuid"
},
"type": {
"type": "string"
},
"name": {
"type": "string"
},
"flavours": {
"type": "array",
"items": {
"type": "string"
}
},
"batters": {
"type": "object",
"properties": {
"topping": {
"type": "string"
},
"category": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
}
} Sample Data: {
"id": "4b5260d2-e088-4546-a315-b9c4b274406f",
"type": "donut",
"name":"cake",
"flavours": ["chocolate","blueberry","vanilla"],
"batters": {
"topping":"glazed",
"category":[ "eggless","flavoured"]
}
} Scenario-1 {
"id": "4b5260d2-e088-4546-a315-b9c4b274406f",
"type": "donut",
"name":"cake",
"flavours": " chocolate ; blueberry ; vanilla",
"batters/topping": "glazed",
"batters/category": " eggless;flavoured"
} and I will write this to a CSV or Parquet file. Scenario-2 {
"id": "4b5260d2-e088-4546-a315-b9c4b274406f",
"type": "donut",
"name":"cake",
"flavours": " chocolate ; blueberry ; vanilla",
"batters/topping": "glazed",
"batters/category": " eggless;flavoured"
} So , I want to unflatten it in such a way that it matches my schema. So after Unflatten the data should look like below. {
"id": "4b5260d2-e088-4546-a315-b9c4b274406f",
"type": "donut",
"name":"cake",
"flavours": ["chocolate","blueberry","vanilla"],
"batters": {
"topping":"glazed",
"category":[ "eggless","flavoured"]
}
} I made few changes to the method |
have made enhancement to the library to handle this conversion. Sample fiddle for your review: https://dotnetfiddle.net/gr4u94 |
This works. Thank you @Cinchoo for the enhancement. |
I have these lines of code :
and my Json is
my expected output:
Basic requirement while flattening and unflattening is that, if the value of a property is list or array of primitive type then while flattening we will merge the list or array to a ';' separated string and while unflattening we will split the ';' separated string to a list or array.
Note: the JSON showed here is a sample, originally I have a large JSON array and each object has lot's of properties so using LINQ solution is not feasible. Also I am looking for a generic solution as I have multiple entities of different types with similar properties.(just a change in name)
Eg: cake, chocolate, chips, cookies and so on. SO looking for a generic solution.
Please comment if further details are required.
Thanks for your interest in the question.
Looking forward to hearing from you @Cinchoo @neuli1980 @dbeattie1971
The text was updated successfully, but these errors were encountered: