How to parse two different JSON formats
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
add a comment |
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
What is type1 and type2? The only differences between your two examples (despite the fact that theproject_name
s 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 aprojects
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
add a comment |
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
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
c# asp.net json json.net
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 theproject_name
s 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 aprojects
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
add a comment |
What is type1 and type2? The only differences between your two examples (despite the fact that theproject_name
s 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 aprojects
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_name
s 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_name
s 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
add a comment |
1 Answer
1
active
oldest
votes
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
edited Nov 12 '18 at 17:19
answered Nov 12 '18 at 17:12
Brian RogersBrian Rogers
77k18193208
77k18193208
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
What is type1 and type2? The only differences between your two examples (despite the fact that the
project_name
s 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