I'm trying to understand why JSON.Net will modify the original JObject (formSchema) in two cases below where I'm adding JArray and JObject
to objects I've defined as vars.
I assume the two vars I created were standalone objects in their own right but it appears they are not.
In the first case, requiredItems is a JArray var, in which I want to add the token "certification" to formSchema, but I've placed the Add method
on requiredItems. Not formSchema.
In the second case, I add certifyProperty to formSchema using the Add method on consentProps. The property handily gets added to formSchema
without a direct reference to it.
Why is this? Are they linked in memory? Where in the JSON.Net docs is this explained? I cannot find it.
namespace JSONProps
{
class Program
{
static void Main(string[] args)
{
string jsonSchema = #"
{
""jsonSchema"": {
""type"": ""object"",
""title"": ""a title"",
""properties"": {
""consent"": {
""type"": ""object"",
""title"": ""Consent of Applicant"",
""required"": [
""applicantConsent""
],
""properties"": {
""applicantConsent"": {
""type"": ""boolean"",
""title"": ""I give my Consent"",
},
}
}
}
}
}
";
// First case
var formSchema = JObject.Parse(jsonSchema);
var requiredProps = formSchema["jsonSchema"]["properties"]["consent"]["required"] as JArray;
requiredProps.Add("certification");
// Second case
var consentProps = formSchema["jsonSchema"]["properties"]["consent"]["properties"] as JObject;
var certifyProperty = JObject.Parse(#" { ""type"" : ""boolean"", ""title"" : ""This is true."" } ");
consentProps.Add("certification", certifyProperty);
Console.WriteLine(formSchema.ToString());
}
}
}
$ dotnet run
{
"jsonSchema": {
"type": "object",
"title": "a title",
"properties": {
"consent": {
"type": "object",
"title": "Consent of Applicant",
"required": [
"applicantConsent",
"certification"
],
"properties": {
"applicantConsent": {
"type": "boolean",
"title": "I give my Consent"
},
"certification": {
"type": "boolean",
"title": "This is true."
}
}
}
}
}
}
Related
I am wondering if we can take some parts inside a JSON string and print it out. I have a JSON string and I only want to take some specific things in it.
My JSON string:
{
"A": {
"a1":[
{
"a11": {
"a111": "something1",
"a112": "something2"
},
"a12": [
{
"a121": "something1",
"a122": "something2"
},
{
"a211": "something1",
"a212": "soemthing2"
}
]
]
},
"B": {
"b1":[
{
"b11": {
"b111": "something1",
"b112": "something2"
},
"b12": [
{
"b121": "something1",
"b122": "something2"
},
{
"b211": "something1",
"b212": "soemthing2"
}
]
}
]
}
}
I only want to print out: b121 is something1. How can I do it in C#?
Firstly, your JSON example has some problems so I'll give an example using this part as the JSON file.
{
"B": {
"b1":
{
"b11": {
"b111": "something1",
"b112": "something2"
},
"b12": [
{
"b121": "something1",
"b122": "something2"
},
{
"b211": "something1",
"b212": "soemthing2"
}
]
}
}
}
You can access to a specific part like this using Json.NET:
var jsonData = File.ReadAllText(#"\test.json");
dynamic jsonObject = JsonConvert.DeserializeObject(jsonData);
// print out a specific part
Console.WriteLine(jsonObject.B.b1.b11.b111);
I want to my all json array size is equal,suppose my array limit is 2, in my json many array have less or high json count than 2.if array object size less than 2 add empty array with null values and if greater than 2 remove.
{
"data": {
"getUsers": [
{
"UserProfileDetail": {
"UserStatus": {
"name": "User One"
},
"UserStatusDate": "2018-10-31T06:12:42+00:00",
"EnrollId": "am**********************************",
"lastDate": "2019-07-22T03:05:39.0245313-04:00"
},
"UserInformation": {
"Id": 1111122,
"firstName": "*****",
"middleName": null,
"lastName": "*****",
"otherNames": null,
"primaryState": "MA",
"otherState": [
"MA",
"BA",
"DL",
"RJ"
],
"UserLicense": [
{
"licenseState": "MA",
"licenseNumber": "000000000",
"licenseStatus": null,
"aaaaaaaaaaaaaaaaa": "only one"
},
{
"licenseState": "MA2",
"licenseNumber": "0000000002",
"licenseStatus": null,
"aaaaaaaaaaaaaaaaa": "only one2"
},
{
"licenseState": "MA3",
"licenseNumber": "0000000003",
"licenseStatus": null,
"aaaaaaaaaaaaaaaaa": "only one3"
}
],
"UserLocation": [
{
"location": "DL",
"SubrLocation": [
{
"sub location1": "DL1",
"sub location2": "DL2"
}
]
}
],
"Setting": "ADMINISTRATIVE",
"primaryEmail": "*****#*****.com",
"modifiedAt": null,
"createdAt": null
}
},
{
"UserProfileDetail": {
"UserStatus": {
"name": "User Two"
},
"UserStatusDate": "2019-10-31T06:12:42+00:00",
"EnrollId": "am**********************************",
"lastDate": "2019-07-22T03:05:39.0245313-04:00"
},
"UserInformation": {
"Id": 443333,
"firstName": "*****",
"middleName": "Jhon",
"lastName": "*****",
"otherNames": null,
"primaryState": "AK",
"otherState": [
"MP",
"CLT"
],
"UserLicense": [
{
"licenseState": "KL",
"licenseNumber": "000000220",
"licenseStatus": "Valid"
}
],
"UserLocation": [
{
"location": "KL",
"SubrLocation": [
{
"sub location1": "KL",
"sub location2": "KL2"
},
{
"sub location1": "TN",
"sub location2": "TN2"
}
]
}
],
"Setting": "ADMINISTRATIVE",
"primaryEmail": "*****#*****.com",
"modifiedAt": null,
"createdAt": null
}
}
]
}
}
The above example following array object is found.
1.UserLicense
2.UserLocation
3.SubrLocation
The 'UserLicense' location in first json is 3 and second json is two. I want remove one from first json and add empty json to second. like this i want all json array (including nested json array object).
I know its not a correct requirement/method.
some body please help me
This is a generic question about working with json in .NET.
Option 1
You could create a class model that represents your json, then load it with JsonConvert.DeserializeObject, then make the changes in the model and serialize it back to json with JsonConvert.SerializeObject.
The top two elements would look like this:
public class TopElement
{
[JsonProperty("data", NullValueHandling = NullValueHandling.Ignore)]
public Data Data { get; set; }
}
public class Data
{
[JsonProperty("getUsers", NullValueHandling = NullValueHandling.Ignore)]
public GetUsers[] GetUsers { get; set; }
}
// ... and so on
Deserialize json from string:
var obj = JsonConvert.DeserializeObject<TopElement>(jsonString);
Option 2
You could use directly JObject to load the json and then make changes in it.
I am serializing an anonymous object to use as a request message for an HTTP post request. The problem is that one of the JSON keys contains a dot in its name. VS throws out ''invalid anonymous type member declarator'' error.
return JsonConvert.SerializeObject(new
{
query = "something",
firstname.keyword = "xyz"
});
What can I do to solve this issue?
Edit: the real json request looks something like this, so I don't think I can use a dictionary:
{
"query": {
"bool": {
"must": [
{
"term": {
"firstname.keyword": ""
}
}
],
"must_not": [ ],
"should": [ ]
}
},
"from": 0,
"size": 10,
"sort": [ ],
"aggs": { }
}
Json can generally be represented using arrays, dictionaries and anonymous objects.
The first part of your Json can be generated as follows:
return JsonConvert.SerializeObject(new
{
query = new
{
#bool = new
{
must = new[]
{
new
{
term = new Dictionary<string, object>
{
["firstname.keyword"] = string.Empty,
}
}
}
}
}
});
As you can see below, in the json i have "data", "data2" and invoice.
My goal is to split a json file in more parts that have "data" and "data2" and a part of "invoice".
I've de-serialized the string, that contain the json code, in a dynamic object so i need to make a partial copy of this in a temp dynamic object, serialize it and than continue my job.
string strJsonOuput = Newtonsoft.Json.JsonConvert.SerializeXmlNode(xmlInput);
dynamic objComune = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(strJsonOuput);
This an example of the json:
{
"data": {
"a": "1",
"b": "2"
},
"data2": {
"something1": {
"thing1": "xxx",
},
"Invoice": [
{
"head": "bbb",
"body": {
"argument1":"aaa"
}
}
{
"head": "xxxx",
"body": {
"argument2": "ccc"
}
}
{
"head": "xxxx",
"body": {
"argument2": "ccc"
}
}
{...}
]
}
}
Below is my JSON:
[
{
"name": "Node-1",
"flag": true,
"myObj": {
region: {
info: {
name: null,
infoFlag: false,
}
}
},
"nodes": [
{
"name": "Node-1-1",
"flag": true,
"myObj": {
region: {
info: {
name: "abc",
infoFlag: false,
}
}
},
"nodes": [
{
"name": "Node-1-1-1",
"flag": true,
"myObj": {
region: {
info: {
name: "xyz",
infoFlag: false,
}
}
},
"nodes": [
]
}
]
}
]
}
]
I want to update two properties of my above JSON string with following rule :
Flag : I want to blindly update this property to false.
infoFlag: If name property of info is null then I want to update infoFlag to true else false if it is not null.
So after updating my JSON with these rules I want to have that JSON as a string.
Note: I don't want to deserialize and then update property based on above two rules as because my JSON has lots of properties for which I don't want to create classes, so I am looking for something which will work without deserializing with class.
This is how I am trying to do it:
string json = "MyJson";
var temp = JArray.Parse(json);
temp.Descendants()
.OfType<JProperty>()
json = temp.ToString();
But here I am not getting as how to traverse my JSON recursively; as you can see I have recursive structure like below :
Node-1
Node-1-1
Node-1-1-1
Json.NET allows you to treat its internal objects representing the JSON content as dynamic, which makes the task in question no harder than using regular typed objects.
The only kind of tough problem is the recursive object structure (the nodes array), but that's not a JSON or dynamic specific problem, and can be solved in many ways - the obvious recursive method or with my preferable tree flattening enumeration (the Expand method from my answer to How to flatten tree via LINQ?).
With that being said, the solution could be like this:
var array = JArray.Parse(json);
var nodes = array.Cast<dynamic>().Expand(x => x.nodes);
foreach (var node in nodes)
{
node.flag = true;
var info = node.myObj.region.info;
info.infoFlag = (info.name == null);
}
var newJson = array.ToString();
If I have understood your needs this code is pretty verbose and not so elegant but works:
JArray temp = JArray.Parse(json);
foreach (JToken tk in temp.Descendants())
{
if (tk.Type == JTokenType.Property)
{
JProperty p = tk as JProperty;
if (p.Name == "flag")
{
if ((bool)p.Value.ToObject(typeof(bool)) == true)
p.Value = false;
}
if ((p.Name == "info") && p.HasValues)
{
bool flag = false;
foreach (JToken tkk in p.Descendants())
{
if (tkk.Type == JTokenType.Property)
{
JProperty pp = tkk as JProperty;
if ((pp.Name == "name") && (pp.Value.Type == JTokenType.Null))
{
flag = true;
}
if ((pp.Name == "infoFlag"))
{
pp.Value = (flag == true) ? true : false;
}
}
}
}
}
}
json = temp.ToString();
This is the resulting output:
[
{
"name": "Node-1",
"flag": false,
"myObj": {
"region": {
"info": {
"name": null,
"infoFlag": true
}
}
},
"nodes": [
{
"name": "Node-1-1",
"flag": false,
"myObj": {
"region": {
"info": {
"name": "abc",
"infoFlag": false
}
}
},
"nodes": [
{
"name": "Node-1-1-1",
"flag": false,
"myObj": {
"region": {
"info": {
"name": "xyz",
"infoFlag": false
}
}
},
"nodes": []
}
]
}
]
}
]