1

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?

asked Feb 11, 2013 at 18:32

1 Answer 1

3

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.

answered Feb 11, 2013 at 20:24

7 Comments

This is blowing up for me on the following line: var primitiveValue = value["primitiveValue"]; "Cannot access child value on Newtonsoft.Json.Linq.JProperty."
@wloescher What does the JSON look like? It sounds like that error is coming from trying to access a child when you have a property.
i'm getting the same error at the same place. it happens on the first instance of trying to evaluate that line. i'm running this code on the whole JSON document which includes other elements inside the root but not in rows. I thought that could be the reason for error, but it cant be if wloescher is trying it with the example fragment above
@AlistairMcIntyre I have updated my code sample with the code I am using to parse the JSON (the first two lines of code). Try that and see if it works.
@wloescher That edit might help solve the problem you are having also.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.