Home > Enterprise >  Removing header from JSON but keeping data C#
Removing header from JSON but keeping data C#

Time:06-26

I currently have this JSON file.

{   
"result": {
  "records": [
  {
    "_id": 477000,
    "HourUTC": "2017-01-01T00:00:00 01:00",
    "HourDK": "2017-01-01T00:00:00",
    "PriceArea": "SE4",
    "SpotPriceDKK": 178.64,
    "SpotPriceEUR": 24.03
  },
  {
    "_id": 477001,
    "HourUTC": "2017-01-01T01:00:00 01:00",
    "HourDK": "2017-01-01T01:00:00",
    "PriceArea": "SE4",
    "SpotPriceDKK": 178.64,
    "SpotPriceEUR": 24.03
  },
  {
    "_id": 477002,
    "HourUTC": "2017-01-01T02:00:00 01:00",
    "HourDK": "2017-01-01T02:00:00",
    "PriceArea": "SE4",
    "SpotPriceDKK": 178.56,
    "SpotPriceEUR": 24.02
  },
  {
    "_id": 477003,
    "HourUTC": "2017-01-01T03:00:00 01:00",
    "HourDK": "2017-01-01T03:00:00",
    "PriceArea": "SE4",
    "SpotPriceDKK": 172.39,
    "SpotPriceEUR": 23.19
  },
  {
    "_id": 477004,
    "HourUTC": "2017-01-01T04:00:00 01:00",
    "HourDK": "2017-01-01T04:00:00",
    "PriceArea": "SE4",
    "SpotPriceDKK": 179.16,
    "SpotPriceEUR": 24.1
  }
]}}

How would i go about clearing the Headers so that i only have the data like this? Everything ive tried ends up removing the data with the Header.

  [{
    "_id": 477000,
    "HourUTC": "2017-01-01T00:00:00 01:00",
    "HourDK": "2017-01-01T00:00:00",
    "PriceArea": "SE4",
    "SpotPriceDKK": 178.64,
    "SpotPriceEUR": 24.03
  },
  {
    "_id": 477001,
    "HourUTC": "2017-01-01T01:00:00 01:00",
    "HourDK": "2017-01-01T01:00:00",
    "PriceArea": "SE4",
    "SpotPriceDKK": 178.64,
    "SpotPriceEUR": 24.03
  },
  {
    "_id": 477002,
    "HourUTC": "2017-01-01T02:00:00 01:00",
    "HourDK": "2017-01-01T02:00:00",
    "PriceArea": "SE4",
    "SpotPriceDKK": 178.56,
    "SpotPriceEUR": 24.02
  },
  {
    "_id": 477003,
    "HourUTC": "2017-01-01T03:00:00 01:00",
    "HourDK": "2017-01-01T03:00:00",
    "PriceArea": "SE4",
    "SpotPriceDKK": 172.39,
    "SpotPriceEUR": 23.19
  },
  {
    "_id": 477004,
    "HourUTC": "2017-01-01T04:00:00 01:00",
    "HourDK": "2017-01-01T04:00:00",
    "PriceArea": "SE4",
    "SpotPriceDKK": 179.16,
    "SpotPriceEUR": 24.1
  }
]

I tried creating a class and parsing it over, ive also tried working it with regex but i cant seem to find any solution i hope someone might be abit more awake then i am :)

CodePudding user response:

The simplest way is probably to just parse the existing document as a JObject, then use the indexer to drill down through the result and records properties:

using Newtonsoft.Json.Linq;

string json = File.ReadAllText("test.json");
JObject obj = JObject.Parse(json);
var array = obj["result"]["records"];
Console.WriteLine(array);
// Or use array.ToString() to get the JSON for further work.

You could also cast the property value to JArray if you want to use individual elements of it:

using Newtonsoft.Json.Linq;

string json = File.ReadAllText("test.json");
JObject obj = JObject.Parse(json);
var array = (JArray) obj["result"]["records"];

Of course, this assumes you still want it as JSON. If you just want to use the JSON file for in-memory objects, I'd suggest creating a class structure like this:

public class Root
{
    [JsonProperty("result")]
    public Result Result { get; set; }
}

public class Result
{
    [JsonProperty("records")]
    public List<Record> Records { get; set; }
}

public class Record
{
    [JsonProperty("_id")]
    public int Id { get; set; }

    [JsonProperty("HourUtc")]
    public DateTimeOffset Timestamp { get; set; }

    [JsonProperty("HourDK")]
    public DateTime LocalTime { get; set; }

    [JsonProperty("PriceArea")]
    public string PriceArea { get; set; }

    [JsonProperty("SpotPriceDKK")]
    public decimal SpotPriceKrona { get; set; }

    [JsonProperty("SpotPriceEUR")]
    public decimal SpotPriceEuros { get; set; }
}

Then you can just use var root = JsonConvert.DeserializeObject<Root>(json); and go from there...

CodePudding user response:

the easiest way would be just parse your json

using Newtonsoft.Json;

var jsonParsed = (JArray) JObject.Parse(json)["result"]["records"];

if you need an updated json

json= jsonParsed.ToString();

if you would like to convert it to c# instance, you will have to create a class and deserialize your json parsed

 List<Price> records = jsonParsed.ToObject<List<Price>>();

//or from the start

List<Price> records = JObject.Parse(json)["result"]["records"].ToObject<List<Price>>();

class

public class Price
{
        [JsonProperty("_id")]
        public int Id { get; set; }
        public DateTime HourUTC { get; set; }
        public DateTime HourDK { get; set; }
        public string PriceArea { get; set; }
        public double SpotPriceDKK { get; set; }
        public double SpotPriceEUR { get; set; }
}
  • Related