I am new to .NET and trying to achieve what shape-json does in the Node world.
Copied from the shape-json npm page:
Given the following array of objects:
var input = [
{pid: 1, contributor: 'jdalton', projectID: 1, projectName: 'lodash'},
{pid: 1, contributor: 'jdalton', projectID: 2, projectName: 'docdown'},
{pid: 1, contributor: 'jdalton', projectID: 3, projectName: 'lodash-cli'},
{pid: 2, contributor: 'contra', projectID: 4, projectName: 'gulp'},
{pid: 3, contributor: 'phated', projectID: 4, projectName: 'gulp'},
]
You can apply a schema like this:
var scheme = {
"$group[contributors](pid)": {
"id": "pid",
"name": "contributor",
"$group[projects](projectID)": {
"id": "projectID",
"name": "projectName"
}
}
};
To get a nested JSON like this:
{
"contributors": [
{
"id": 1,
"name": "jdalton",
"projects": [
{
"id": 1,
"name": "lodash"
},
{
"id": 2,
"name": "docdown"
},
{
"id": 3,
"name": "lodash-cli"
}
]
},
{
"id": 2,
"name": "contra",
"projects": [
{
"id": 4,
"name": "gulp"
}
]
},
{
"id": 3,
"name": "phated",
"projects": [
{
"id": 4,
"name": "gulp"
}
]
}
]
}
In my case, I have a product hierarchy (product class -> product subclass -> product) which I am trying to represent as a nested JSON.
Background:
I am getting the data from a MySQL table and reading it into a
MySqlDataReader
.Following this SO question, I have been able to serialize the data into a JSON
I am clueless on how to create a nested JSON like the example above. Please advise.