Home > other >  JsonReaderException being throw trying to parse a JSON file that contains several JSON objects insid
JsonReaderException being throw trying to parse a JSON file that contains several JSON objects insid

Time:03-26

I exported from Azure IoT Central to a Blob Storage a file containing several JSON objects (36 objects) within that same file.

The below are the first 2 lines from that file

{"applicationId":"appID","component":"thermostat1","deviceId":"usingTemControllerTemplate","enqueuedTime":"2022-03-21T15:31:38.687Z","enrichments":{},"messageProperties":{},"messageSource":"telemetry","schema":"default@v1","telemetry":{"temperature":23.2},"templateId":"urn:modelDefinition:tczx6jwcwz1:h2httvyo48g"}

{"applicationId":"appID","component":"thermostat2","deviceId":"usingTemControllerTemplate","enqueuedTime":"2022-03-21T15:31:38.703Z","enrichments":{},"messageProperties":{},"messageSource":"telemetry","schema":"default@v1","telemetry":{"temperature":16.9},"templateId":"urn:modelDefinition:tczx6jwcwz1:h2httvyo48g"}

I created 2 classes to show the heirarchy in the JSON objects. RootObject & Telemetry.

public class RootObject
{
    public string applicationId { get; set; }
    public string component { get; set; }
    public string deviceId { get; set; }
    public string enqueuedTime { get; set; }
    public string messageSource { get; set; }
    public string schema { get; set; }
    public List<Telemetry> telemetry { get; set; }
    public string templateId { get; set; }
}

public class Telemetry
{
    public double temperature { get; set; }
}

I followed JsonReaderException thrown

Could someone please help me find the cause of this issue and how I could resolve it?

CodePudding user response:

first of all the JSON format is wrong, it looks like this (see below) and secondly he doesn't want to have the File path but the json (value), so you have to read it in and what is also very important. You have 2 elements of "RootObject", that means you have to put into a Array or List<> of RootObjects

Code:

using Newtonsoft.Json;

//reads the file and saves into a string
string jsonValue = File.ReadAllText("~pathToFile");

//Deserialize the objects and put them in a list of "RootObject".
List<RootObject> rt = JsonConvert.DeserializeObject<List<RootObject>>(jsonValue);

correct JSON format:

[
     {
          "applicationId": "appID",
          "component": "thermostat1",
          "deviceId": "usingTemControllerTemplate",
          "enqueuedTime": "2022-03-21T15:31:38.687Z",
          "enrichments": {},
          "messageProperties": {},
          "messageSource": "telemetry",
          "schema": "default@v1",
          "telemetry": {
               "temperature": 23.2
          },
          "templateId": "urn:modelDefinition:tczx6jwcwz1:h2httvyo48g"
     },
     {
          "applicationId": "appID",
          "component": "thermostat2",
          "deviceId": "usingTemControllerTemplate",
          "enqueuedTime": "2022-03-21T15:31:38.703Z",
          "enrichments": {},
          "messageProperties": {},
          "messageSource": "telemetry",
          "schema": "default@v1",
          "telemetry": {
               "temperature": 16.9
          },
          "templateId": "urn:modelDefinition:tczx6jwcwz1:h2httvyo48g"
     }
]

CodePudding user response:

you have to fix json, by converting it to array of objects

    var json = File.ReadAllText(filePath);
    json = "["   json.Replace("\n\r",",") "]";

    List<RootObject> lrt = JsonConvert.DeserializeObject<List<RootObject>>(json);
    
    double[] telemetries=rt.Select(r => r.telemetry.temperature ).ToArray(); // [23.2,16.9]
    
    double telemetry=rt.Where(t=> t.telemetry.temperature==23.2)
                            .Select(r =>r.telemetry.temperature ).FirstOrDefault(); //23.2

and fix class too, telemetry should be an object, not a list

public class RootObject
{
    ....
    public Telemetry telemetry { get; set; }
}

CodePudding user response:

This file is not a list/array of objects, it's a 36 lines with each line containing json for a single object.

With this observation we can:

var lines = File.ReadAllLines(filePath);

List<RootObject> list = new();
foreach(var line in lines)
{
   RootObject o = JsonConvert.DeserializeObject<RootObject>(filePath);
   list.Add(o);
}
  • Related