How to parse two different JSON formats










0















I need to parse 2 different types of JSONs as shown below:



JSON 1:




"projects": [

"sno": "1",
"project_name": "Abs",
"project_Status": "Live"
,

"sno": "2",
"project_name": "Cgi",
"project_Status": "Live"

]



JSON 2:



[

"sno": "1",
"project_name": "Disc",
"project_Status": "Live"
,

"sno": "2",
"project_name": "Rol",
"project_Status": "Live"

]


I was parsing the JSON 2 as follows:



using (StreamReader streamReader = new StreamReader(Path.Combine(Path.GetTempPath(), "sample.json")))
using (JsonTextReader reader = new JsonTextReader(streamReader))

var serializer = new JsonSerializer();
while (reader.Read())

if (reader.TokenType == JsonToken.StartObject)

JObject jsonPayload = JObject.Load(reader);
jsonProfile = jsonPayload.ToString();
JObject json = JObject.Parse(jsonProfile);





Is it possible for me to modify this to check if the JSON is in type 1 or type 2 and then parse it to assign each project to a different JObject?










share|improve this question
























  • What is type1 and type2? The only differences between your two examples (despite the fact that the project_names are different), is that the first has a { in front. Is this what you mean by different types?

    – HimBromBeere
    Nov 12 '18 at 11:38







  • 3





    You already parse the object in the first line. Why parse it again? Besides, the first string will return an object (JObject) with a projects property while the second will return a JArray. The snippet works only because a JArray inherits from JObject. The only change needed is to use JToken instead of JObject

    – Panagiotis Kanavos
    Nov 12 '18 at 11:38












  • @HimBromBeere when I am parsing the JSON 2 with the code given, the entire JSON is getting assigned to a single JObject. Due to this, I am unable to separate the different projects

    – Harry
    Nov 12 '18 at 12:21















0















I need to parse 2 different types of JSONs as shown below:



JSON 1:




"projects": [

"sno": "1",
"project_name": "Abs",
"project_Status": "Live"
,

"sno": "2",
"project_name": "Cgi",
"project_Status": "Live"

]



JSON 2:



[

"sno": "1",
"project_name": "Disc",
"project_Status": "Live"
,

"sno": "2",
"project_name": "Rol",
"project_Status": "Live"

]


I was parsing the JSON 2 as follows:



using (StreamReader streamReader = new StreamReader(Path.Combine(Path.GetTempPath(), "sample.json")))
using (JsonTextReader reader = new JsonTextReader(streamReader))

var serializer = new JsonSerializer();
while (reader.Read())

if (reader.TokenType == JsonToken.StartObject)

JObject jsonPayload = JObject.Load(reader);
jsonProfile = jsonPayload.ToString();
JObject json = JObject.Parse(jsonProfile);





Is it possible for me to modify this to check if the JSON is in type 1 or type 2 and then parse it to assign each project to a different JObject?










share|improve this question
























  • What is type1 and type2? The only differences between your two examples (despite the fact that the project_names are different), is that the first has a { in front. Is this what you mean by different types?

    – HimBromBeere
    Nov 12 '18 at 11:38







  • 3





    You already parse the object in the first line. Why parse it again? Besides, the first string will return an object (JObject) with a projects property while the second will return a JArray. The snippet works only because a JArray inherits from JObject. The only change needed is to use JToken instead of JObject

    – Panagiotis Kanavos
    Nov 12 '18 at 11:38












  • @HimBromBeere when I am parsing the JSON 2 with the code given, the entire JSON is getting assigned to a single JObject. Due to this, I am unable to separate the different projects

    – Harry
    Nov 12 '18 at 12:21













0












0








0








I need to parse 2 different types of JSONs as shown below:



JSON 1:




"projects": [

"sno": "1",
"project_name": "Abs",
"project_Status": "Live"
,

"sno": "2",
"project_name": "Cgi",
"project_Status": "Live"

]



JSON 2:



[

"sno": "1",
"project_name": "Disc",
"project_Status": "Live"
,

"sno": "2",
"project_name": "Rol",
"project_Status": "Live"

]


I was parsing the JSON 2 as follows:



using (StreamReader streamReader = new StreamReader(Path.Combine(Path.GetTempPath(), "sample.json")))
using (JsonTextReader reader = new JsonTextReader(streamReader))

var serializer = new JsonSerializer();
while (reader.Read())

if (reader.TokenType == JsonToken.StartObject)

JObject jsonPayload = JObject.Load(reader);
jsonProfile = jsonPayload.ToString();
JObject json = JObject.Parse(jsonProfile);





Is it possible for me to modify this to check if the JSON is in type 1 or type 2 and then parse it to assign each project to a different JObject?










share|improve this question
















I need to parse 2 different types of JSONs as shown below:



JSON 1:




"projects": [

"sno": "1",
"project_name": "Abs",
"project_Status": "Live"
,

"sno": "2",
"project_name": "Cgi",
"project_Status": "Live"

]



JSON 2:



[

"sno": "1",
"project_name": "Disc",
"project_Status": "Live"
,

"sno": "2",
"project_name": "Rol",
"project_Status": "Live"

]


I was parsing the JSON 2 as follows:



using (StreamReader streamReader = new StreamReader(Path.Combine(Path.GetTempPath(), "sample.json")))
using (JsonTextReader reader = new JsonTextReader(streamReader))

var serializer = new JsonSerializer();
while (reader.Read())

if (reader.TokenType == JsonToken.StartObject)

JObject jsonPayload = JObject.Load(reader);
jsonProfile = jsonPayload.ToString();
JObject json = JObject.Parse(jsonProfile);





Is it possible for me to modify this to check if the JSON is in type 1 or type 2 and then parse it to assign each project to a different JObject?







c# asp.net json json.net






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 17:26









Brian Rogers

77k18193208




77k18193208










asked Nov 12 '18 at 11:34









HarryHarry

207




207












  • What is type1 and type2? The only differences between your two examples (despite the fact that the project_names are different), is that the first has a { in front. Is this what you mean by different types?

    – HimBromBeere
    Nov 12 '18 at 11:38







  • 3





    You already parse the object in the first line. Why parse it again? Besides, the first string will return an object (JObject) with a projects property while the second will return a JArray. The snippet works only because a JArray inherits from JObject. The only change needed is to use JToken instead of JObject

    – Panagiotis Kanavos
    Nov 12 '18 at 11:38












  • @HimBromBeere when I am parsing the JSON 2 with the code given, the entire JSON is getting assigned to a single JObject. Due to this, I am unable to separate the different projects

    – Harry
    Nov 12 '18 at 12:21

















  • What is type1 and type2? The only differences between your two examples (despite the fact that the project_names are different), is that the first has a { in front. Is this what you mean by different types?

    – HimBromBeere
    Nov 12 '18 at 11:38







  • 3





    You already parse the object in the first line. Why parse it again? Besides, the first string will return an object (JObject) with a projects property while the second will return a JArray. The snippet works only because a JArray inherits from JObject. The only change needed is to use JToken instead of JObject

    – Panagiotis Kanavos
    Nov 12 '18 at 11:38












  • @HimBromBeere when I am parsing the JSON 2 with the code given, the entire JSON is getting assigned to a single JObject. Due to this, I am unable to separate the different projects

    – Harry
    Nov 12 '18 at 12:21
















What is type1 and type2? The only differences between your two examples (despite the fact that the project_names are different), is that the first has a { in front. Is this what you mean by different types?

– HimBromBeere
Nov 12 '18 at 11:38






What is type1 and type2? The only differences between your two examples (despite the fact that the project_names are different), is that the first has a { in front. Is this what you mean by different types?

– HimBromBeere
Nov 12 '18 at 11:38





3




3





You already parse the object in the first line. Why parse it again? Besides, the first string will return an object (JObject) with a projects property while the second will return a JArray. The snippet works only because a JArray inherits from JObject. The only change needed is to use JToken instead of JObject

– Panagiotis Kanavos
Nov 12 '18 at 11:38






You already parse the object in the first line. Why parse it again? Besides, the first string will return an object (JObject) with a projects property while the second will return a JArray. The snippet works only because a JArray inherits from JObject. The only change needed is to use JToken instead of JObject

– Panagiotis Kanavos
Nov 12 '18 at 11:38














@HimBromBeere when I am parsing the JSON 2 with the code given, the entire JSON is getting assigned to a single JObject. Due to this, I am unable to separate the different projects

– Harry
Nov 12 '18 at 12:21





@HimBromBeere when I am parsing the JSON 2 with the code given, the entire JSON is getting assigned to a single JObject. Due to this, I am unable to separate the different projects

– Harry
Nov 12 '18 at 12:21












1 Answer
1






active

oldest

votes


















1














Unless your JSON is large (thousands of lines), I would dispense with the reader altogether. Instead, read the whole JSON file into a string using File.ReadAllText and parse it using JToken.Parse. From there it is easy to check whether you have an array (JSON 2) or an object containing an array (JSON 1) and then process accordingly:



string fileName = Path.Combine(Path.GetTempPath(), "sample.json");
string json = File.ReadAllText(fileName);
JToken token = JToken.Parse(json);
JArray array = (token.Type == JTokenType.Array) ? (JArray)token : (JArray)token["projects"];
foreach (JObject project in array)

Console.WriteLine("number: " + (string)project["sno"]);
Console.WriteLine("name: " + (string)project["project_name"]);
Console.WriteLine("status: " + (string)project["project_Status"]);
Console.WriteLine();



Fiddle: https://dotnetfiddle.net/lA87Xo






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261323%2fhow-to-parse-two-different-json-formats%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Unless your JSON is large (thousands of lines), I would dispense with the reader altogether. Instead, read the whole JSON file into a string using File.ReadAllText and parse it using JToken.Parse. From there it is easy to check whether you have an array (JSON 2) or an object containing an array (JSON 1) and then process accordingly:



    string fileName = Path.Combine(Path.GetTempPath(), "sample.json");
    string json = File.ReadAllText(fileName);
    JToken token = JToken.Parse(json);
    JArray array = (token.Type == JTokenType.Array) ? (JArray)token : (JArray)token["projects"];
    foreach (JObject project in array)

    Console.WriteLine("number: " + (string)project["sno"]);
    Console.WriteLine("name: " + (string)project["project_name"]);
    Console.WriteLine("status: " + (string)project["project_Status"]);
    Console.WriteLine();



    Fiddle: https://dotnetfiddle.net/lA87Xo






    share|improve this answer





























      1














      Unless your JSON is large (thousands of lines), I would dispense with the reader altogether. Instead, read the whole JSON file into a string using File.ReadAllText and parse it using JToken.Parse. From there it is easy to check whether you have an array (JSON 2) or an object containing an array (JSON 1) and then process accordingly:



      string fileName = Path.Combine(Path.GetTempPath(), "sample.json");
      string json = File.ReadAllText(fileName);
      JToken token = JToken.Parse(json);
      JArray array = (token.Type == JTokenType.Array) ? (JArray)token : (JArray)token["projects"];
      foreach (JObject project in array)

      Console.WriteLine("number: " + (string)project["sno"]);
      Console.WriteLine("name: " + (string)project["project_name"]);
      Console.WriteLine("status: " + (string)project["project_Status"]);
      Console.WriteLine();



      Fiddle: https://dotnetfiddle.net/lA87Xo






      share|improve this answer



























        1












        1








        1







        Unless your JSON is large (thousands of lines), I would dispense with the reader altogether. Instead, read the whole JSON file into a string using File.ReadAllText and parse it using JToken.Parse. From there it is easy to check whether you have an array (JSON 2) or an object containing an array (JSON 1) and then process accordingly:



        string fileName = Path.Combine(Path.GetTempPath(), "sample.json");
        string json = File.ReadAllText(fileName);
        JToken token = JToken.Parse(json);
        JArray array = (token.Type == JTokenType.Array) ? (JArray)token : (JArray)token["projects"];
        foreach (JObject project in array)

        Console.WriteLine("number: " + (string)project["sno"]);
        Console.WriteLine("name: " + (string)project["project_name"]);
        Console.WriteLine("status: " + (string)project["project_Status"]);
        Console.WriteLine();



        Fiddle: https://dotnetfiddle.net/lA87Xo






        share|improve this answer















        Unless your JSON is large (thousands of lines), I would dispense with the reader altogether. Instead, read the whole JSON file into a string using File.ReadAllText and parse it using JToken.Parse. From there it is easy to check whether you have an array (JSON 2) or an object containing an array (JSON 1) and then process accordingly:



        string fileName = Path.Combine(Path.GetTempPath(), "sample.json");
        string json = File.ReadAllText(fileName);
        JToken token = JToken.Parse(json);
        JArray array = (token.Type == JTokenType.Array) ? (JArray)token : (JArray)token["projects"];
        foreach (JObject project in array)

        Console.WriteLine("number: " + (string)project["sno"]);
        Console.WriteLine("name: " + (string)project["project_name"]);
        Console.WriteLine("status: " + (string)project["project_Status"]);
        Console.WriteLine();



        Fiddle: https://dotnetfiddle.net/lA87Xo







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 12 '18 at 17:19

























        answered Nov 12 '18 at 17:12









        Brian RogersBrian Rogers

        77k18193208




        77k18193208





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261323%2fhow-to-parse-two-different-json-formats%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

            Crossroads (UK TV series)

            ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế