I have a dataset in JSON and using C# JSON.net library i'm trying to read the data as concatenated string, but having trouble extracting the data. The data has root element, then rows. For each row within "rows" I want to pull out a list of ["conversionPathValue"]["nodeValue"] values and concatenate them together, then concatenate them with the primitiveValue values. Example code of 2 rows is below:
"rows": [
[
{
"conversionPathValue": [
{
"interactionType": "CLICK",
"nodeValue": "MET"
}
]
},
{
"primitiveValue": "20130122"
},
{
"primitiveValue": "000"
},
{
"primitiveValue": "000001"
},
{
"primitiveValue": "000"
},
{
"primitiveValue": "11"
},
{
"primitiveValue": "7290.521799"
}
],
[
{
"conversionPathValue": [
{
"interactionType": "CLICK",
"nodeValue": "MET"
},
{
"interactionType": "CLICK",
"nodeValue": "MET"
},
{
"interactionType": "CLICK",
"nodeValue": "MET"
},
{
"interactionType": "CLICK",
"nodeValue": "MET"
},
{
"interactionType": "CLICK",
"nodeValue": "MET"
},
{
"interactionType": "CLICK",
"nodeValue": "MET"
},
{
"interactionType": "CLICK",
"nodeValue": "MET"
},
{
"interactionType": "CLICK",
"nodeValue": "MET"
},
{
"nodeValue": "(none)"
},
{
"nodeValue": "(none)"
},
{
"interactionType": "CLICK",
"nodeValue": "organic"
}
]
},
{
"primitiveValue": "20130122"
},
{
"primitiveValue": "000"
},
{
"primitiveValue": "000011"
},
{
"primitiveValue": "005"
},
{
"primitiveValue": "1"
},
{
"primitiveValue": "1628.0"
}
],
.....etc........
Using the following code: (jsonExtract is a JObject)
var rows = jsonExtract["root"]["rows"][0].Children();
foreach (JToken result in rows)
{
var primitiveValues = result["primitiveValue"].Values<string>();
var pathValues = result["conversionPathValue"].Values<string>();
string joinedprimitiveValues = string.Join(",", primitiveValues);
string joinedpathValues = string.Join("-", pathValues);
file2.WriteLine(joinedpathValues + ", " + joinedprimitiveValues);
}
This gives an error of "Object reference not set to an instance of an object" when setting primitiveValues.
I know the problem is probably down to the nested elements, but I don't know how to cater for these. Can anyone assist please?