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?
1 Answer 1
I think the problem you were having in your code was that you weren't handling the case where either primitiveValues
or pathValues
is null, which because of the way you are addressing the rows, will actually occur on every iteration.
E.g., if this is a row:
{
"primitiveValue": "20130122"
},
Then result["conversionPathValue"]
is going to return null
.
Try this:
var obj = JObject.Parse(json);
var rows = obj["rows"].Children();
foreach (JToken row in rows)
{
List<string> conversionPaths = new List<string>();
List<string> primitiveValues = new List<string>();
foreach (JToken value in row.Children())
{
var primitiveValue = value["primitiveValue"];
if (primitiveValue != null)
{
primitiveValues.Add(primitiveValue.Value<string>());
}
var conversionPathValue = value["conversionPathValue"];
if (conversionPathValue != null)
{
var paths = conversionPathValue.Children()
.Where(t => t["nodeValue"] != null)
.Select(t => t["nodeValue"].Value<string>());
conversionPaths.AddRange(paths);
}
}
Console.WriteLine("Primitive Values:");
Console.WriteLine(string.Join(",", primitiveValues.ToArray()));
Console.WriteLine("Conversion Paths:");
Console.WriteLine(string.Join(",", conversionPaths.ToArray()) + "\n");
}
When I run that on the given JSON, it produces the following:
Primitive Values:
20130122,000,000001,000,11,7290.521799
Conversion Paths:
MET
Primitive Values:
20130122,000,000011,005,1,1628.0
Conversion Paths:
MET,MET,MET,MET,MET,MET,MET,MET,(none),(none),organic
Note that I added {
and }
around the JSON to get it to work.