none
How to create JSON file using C#/JSON.NET?

    Question

  • Hi All,

    What is best way to create JSON file(1-3MB) using JSONTextWriter or SerializeObject method in JSON.Net(Newtonsoft)?

    Please give me the sample C# code for creating below JSON file;

    {
        "$schema": "http://schema.aa.com/schemas/2015-05-01/mySchema1.json#",
        "contentVersion": "1.0.0.0",

         "Engine": {
            "cc": "2500",
            "type": "boxer"
            },

        "parameters": {
            "Name": {
                "type": "String"
            },
            "Id": {
                "type": "String"
            }

          }

    }

    Thanks,

    Satheesh


    Friday, March 31, 2017 5:13 AM

All replies

  • http://www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_Linq_JObject_Parse.htm

    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Friday, March 31, 2017 9:32 AM
  • Hi,

    There are many many ways to achieve your goal, the easiest way requires four steps:

    1. Create a class hierarchy that represents the structure you want to serialise - this can be done using POCO's or dynamic types

    2. Construct an object hierarchy that accurately matches the data you're looking to store

    3. Serialise that hierarchy to a string using JsonConvert.ToString

    4. Use File.WriteAllText to write the resulting string to a file

    Regards,

    D

    • Proposed as answer by bowman_d Friday, March 31, 2017 11:34 AM
    • Unproposed as answer by SatheeshKumar S Friday, March 31, 2017 12:27 PM
    Friday, March 31, 2017 11:34 AM
  • Thanks Bowman_D. I'm new to JSON, so please share the few links to achieve it.

    My requirement is to create one template JSON file(1-3MB) for azure deployment using c#. The Josn file looks like below; I have tried to create below Json file using different ways - using JSONTextWriter and  SerializeObject. I have completed it using JSONTextWriter. But I have faced the problem while creating "$schema" pair value using SerializeObject.

    {
        "$schema": "http://schema.aa.com/schemas/2015-05-01/mySchema1.json#",
        "contentVersion": "1.0.0.0",
        "parameters": {
            "Name": {
                "type": "String"
            },
            "Id": {
                "type": "String"
            }
          }
    //............
    //............
    }

    Here I want to confirm the best way to create Json file using - JSONTextWriter / SerializeObject?. Which method is suitable for large Json file like 1-3MB?.

    Please give me the sample c# code to build the above Json file include class hierarchy and object hierarchy. It will help me lot.

    Thanks,

    Satheesh



    Friday, March 31, 2017 12:47 PM
  • Hi

    I'm not entirely clear on what you mean by 'azure deployment' in your question.

    Also 'best' is a difficult judgement to make. JSONTextWriter allows you to write JSON in a freeform way, but you'll be writing a lot of code for each node. In contrast, when you create a class hierarchy to structure your output, the serializer (JsonConvert.SerializeObject) will handle all of that for you. So it really comes down to how you are going to use this information.

    POCO - since I'm doing your homework :0) would look something like:

    public class ParameterSetting {
        [JsonProperty(PropertyName="type")]
        public string ParameterType {get;set;}
    }
    
    public class ParameterSettings {
        public ParameterSetting Name {get;set;}
    }
    
    class RootJson {
        [JsonProperty(PropertyName="$schema")]
        public string Schema {get;set;}
        [JsonProperty(PropertyName="contentVersion")]    
        public string ContentVersion {get;set;}
        [JsonProperty(PropertyName="parameters")]    
        public ParameterSettings Parameters {get;set;}
    }

    Instantiate the classes then follow my previous post to serialize. I'm going to guess that you've made an error in your specification, I would think it more likely that the "parameters" property should be an array.

    Anyway, don't forget to mark this answer if it helps.

    D

    • Proposed as answer by bowman_d Sunday, April 2, 2017 4:36 PM
    Friday, March 31, 2017 2:51 PM