0

Here is some generic code;

 Dim serializer as New JsonSerializer()
 Dim decoded = serializer.Deserialize(New JsonTextReader(New StreamReader(New FileStream(jsonFilePath, FileMode.Open, FileAccess.Read))))
 Dim possiblyNull = decoded("test")("path")

If test.path does not exist, this throws a NullReferenceException. How in the world can I check if a path exists using NewtonSoft.Json in vb.net? I cannot seem to find anything in the documentation, or in my searches online. This seems like it would be a fairly standard feature to include.

  • 1
    its not a json thing. Check if the json file exists (File.Exists) and/or a Try/Catch, then `jdata = FileReadAllLines(the filepath)` That doesnt look like you are using JSON.NET because it uses shared/static methods: `Dim thing = JsonConvert.DeserializeObject(Of Thing))(jdata)` – Ňɏssa Pøngjǣrdenlarp Oct 02 '16 at 19:21
  • The issue is not with deserializing the file - the file exists. The issue is that the particular path I'm specifying (in this case the path would be test.path) may or may not exist, and I need to check if it does. I've edited my code to show the JsonDeserializer object for clarification. The exception is thrown on line 3 of the code in my OP. – user3613310 Oct 02 '16 at 19:23
  • You are mixing and matching Parsing and Deserializing. use `JObject.Parse(string data)` if you want to parse it – Ňɏssa Pøngjǣrdenlarp Oct 02 '16 at 19:29
  • Why do you say that I'm mixing and matching Parsing and Deserializing? I use deserialize to get a JObject (called decoded), and I'm trying to read a property of the JObject that is located at the path "test.path"? Perhaps a better way to phrase the question would be this: given a json string with unknown structure, how can you check if a node exists at a particular path within that json? – user3613310 Oct 02 '16 at 19:32
  • No, `JsonSerializer.Deserialize()` returns `Object` which should be an instance of a type you defined (not `JObject`). If you want to tip toe thru the parts, parse it as I showed. The debugger will show you the elements – Ňɏssa Pøngjǣrdenlarp Oct 02 '16 at 19:36
  • ...then it just become a matter of `IsNot Nothing` tests – Ňɏssa Pøngjǣrdenlarp Oct 02 '16 at 19:47
  • Ahh ok. Now I originally started this project out using JObject.Parse, however the string is extremely large and it resulted in an out of memory exception (even when debugging for 64 bit). I then switched to the method that the OP was using here: http://stackoverflow.com/questions/33868388/newtonsoft-json-out-of-memory-exception-while-deserializing-big-object - and that is where I failed to realize that Deserialize returns an Object, not a JObject. How would I be able to use streams to keep JObject.Parse from running out of memory? – user3613310 Oct 02 '16 at 19:51
  • JObject.Load did the trick. Plutonix thanks for talking me through this, I truly appreciate the help. – user3613310 Oct 02 '16 at 19:55
  • Finally, to check if the path exists given a `JObject`, you can use [`SelectToken`](http://www.newtonsoft.com/json/help/html/QueryJsonSelectToken.htm): `Dim possiblyNull = decoded.SelectToken("test.path")` – dbc Oct 02 '16 at 20:13

0 Answers0