0

I have a text file that contains json formated text like shows below

[{"products":[{"product_id":"53743443003","quantity":4,"unit_price":42.71}],"value":170.84,"customer":{"id":58}},{"products":[{"product_id":"53743800103","quantity":8,"unit_price":36.78},{"product_id":"frontline-spot-on-Dog-3-1-10","quantity":4,"unit_price":24.62},{"product_id":"53743918500","quantity":7,"unit_price":18.44},{"product_id":"54993530183","quantity":1,"unit_price":57.44},{"product_id":"54993513347","quantity":10,"unit_price":5.0}],"value":629.24,"customer":{"id":18}},{"products":[{"product_id":"53743911105","quantity":8,"unit_price":22.97},{"product_id":"81572503779","quantity":8,"unit_price":63.0},{"product_id":"53743935308","quantity":1,"unit_price":61.96}],"value":749.72,"customer":{"id":53}}

sorry for the long line, but you get the point. this text file is quite large, containing around 200 different "customers"

While working in a console application in c#, i wish to modify the data around so that i can output it into the console as a json file would display, and/ or be able too out put only one type of object, for example "product_id".

products": [
    {
      "product_id": "53743800103",
      "quantity": 8,
      "unit_price": 36.78
    },
    {
      "product_id": "frontline-spot-on-Dog-3-1-10",
      "quantity": 4,
      "unit_price": 24.62
    },
    {
      "product_id": "53743918500",
      "quantity": 7,
      "unit_price": 18.44
    },
    {
      "product_id": "54993530183",
      "quantity": 1,
      "unit_price": 57.44
    },
    {
      "product_id": "54993513347",
      "quantity": 10,
      "unit_price": 5.0
    }
  ],
  "value": 629.24,
  "customer": {
    "id": 18
  }
}

How could i do this?

Florisve
  • 3
  • 2
  • Have you looked into deserializing it with the Json deserializer? Check out this answer: [Deserialize JSON with C#](http://stackoverflow.com/a/7895168/4707373) – Malte R Dec 06 '15 at 21:16
  • Write your text to a [JsonTextWriter](http://www.newtonsoft.com/json/help/html/WriteJsonWithJsonTextWriter.htm) using [WriteRaw](http://www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_JsonTextWriter_WriteRaw.htm) – KiwiPiet Dec 06 '15 at 21:51

1 Answers1

1

Give this a try:

string json = "[{"products":[{"product_id":"53743443003","quantity":4,"unit_price":42.71}],"value":170.84,"customer":{"id":58}},{"products":[{"product_id":"53743800103","quantity":8,"unit_price":36.78},{"product_id":"frontline-spot-on-Dog-3-1-10","quantity":4,"unit_price":24.62},{"product_id":"53743918500","quantity":7,"unit_price":18.44},{"product_id":"54993530183","quantity":1,"unit_price":57.44},{"product_id":"54993513347","quantity":10,"unit_price":5.0}],"value":629.24,"customer":{"id":18}},{"products":[{"product_id":"53743911105","quantity":8,"unit_price":22.97},{"product_id":"81572503779","quantity":8,"unit_price":63.0},{"product_id":"53743935308","quantity":1,"unit_price":61.96}],"value":749.72,"customer":{"id":53}}"

StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);

using (JsonWriter writer = new JsonTextWriter(sw))
{
    writer.Formatting = Formatting.Indented;
    writer.WriteRaw(json);
}

Console.WriteLine(sb.ToString());
KiwiPiet
  • 1,304
  • 14
  • 21