Reading a json file and change it with JSON.NET - c#

JSON is really new to me. How can i use JSON.NET to add a key value pair into a already created json file?
It looks like this:
{
"data": {
"subData1": {
"key1":"value1",
"key2":"value2",
"key3":"value3"
},
"subdata2": {
"key4":"value4",
"key5":"value5",
"key6":"value6"
}
}
"key7":"value7",
"key8":"value8"
}
Say for an example that i want to change it to the following:
{
"data": {
"subData1": {
"key1":"value1",
"key2":"value2",
"key3":"value3"
},
"subdata2": {
"key4":"value4",
"key5":"value5",
"key6":"value6"
},
"newSubData": {
"myKey1":"myVal1",
"myKey2":"myVal2",
"myKey3":"myVal3"
}
}
"key7":"anotherValChangeByMe",
"key8":"value8"
}
Do i need to read the whole JSON file into a dynamic, and then change / add the things i need somehow ?

You can parse the JSON into a JObject, manipulate it via the LINQ-to-JSON API, then get the updated JSON string from the JObject.
For example:
string json = #"
{
""data"": {
""subData1"": {
""key1"": ""value1"",
""key2"": ""value2"",
""key3"": ""value3""
},
""subdata2"": {
""key4"": ""value4"",
""key5"": ""value5"",
""key6"": ""value6""
}
},
""key7"": ""value7"",
""key8"": ""value8""
}";
JObject root = JObject.Parse(json);
JObject data = (JObject)root["data"];
JObject newSubData = new JObject();
newSubData.Add("myKey1", "myValue1");
newSubData.Add("myKey2", "myValue2");
newSubData.Add("myKey3", "myValue3");
data.Add("newSubData", newSubData);
root["key7"] = "anotherValChangeByMe";
Console.WriteLine(root.ToString());
Output:
{
"data": {
"subData1": {
"key1": "value1",
"key2": "value2",
"key3": "value3"
},
"subdata2": {
"key4": "value4",
"key5": "value5",
"key6": "value6"
},
"newSubData": {
"myKey1": "myValue1",
"myKey2": "myValue2",
"myKey3": "myValue3"
}
},
"key7": "anotherValChangeByMe",
"key8": "value8"
}

JSON is ultimately just a string. If you are working on the server side, then unless you want to try to parse out the JSON yourself, the easiest way is to use JSON.NET to deserialize it back into it's native object model. Of course, you can also do this on the client side with JSON.parse(), and add the value there, too.

Related

Get Value From JSON File stored in Azure Storage Using C#

I have JSON file in Azure Storage which I am reading using C#. In that JSON file there is anode called SQLViewDifinition and that node I have SQL which I need to fetch.
I have read the file into a string and converted that string in JObject. I have the JSON now but is finding it difficult to read that particular node. Tried with JToken and Jproperty. But could not crack it.
JSON file looks like this:
{
"jsonSchemaSemanticVersion": "1.4.0",
"imports": [
{
"corpusPath": "cdm:/foundations.cdm.json"
},
{
"corpusPath": "localCdm:/foundations.cdm.json"
}
],
"definitions": [
{
"entityName": "METCredManCollectionGroupEntity",
"exhibitsTraits": [
{
"traitReference": "is.CDM.entityVersion",
"arguments": [
{
"name": "versionNumber",
"value": "1.0.0"
}
]
},
{
"traitReference": "has.sqlViewDefinition",
"arguments": [
{
"name": "sqlViewDefinition",
"value": "CREATE VIEW [DBO].[METCREDMANCOLLECTIONGROUPENTITY] AS SELECT T1.COLLECTIONGROUPID AS COLLECTIONGROUPID, T1.DESCRIPTION AS DESCRIPTION, T1.RECID AS CREDMANCOLLECTIONGROUPTABLERECID, T1.DATAAREAID AS CREDMANCOLLECTIONGROUPTABLEDATAAREAID, T1.RECVERSION AS RECVERSION, T1.PARTITION AS PARTITION, T1.RECID AS RECID FROM CREDMANCOLLECTIONGROUPTABLE T1"
}
]
},
{
"traitReference": "has.backingElements",
"arguments": [
{
"name": "backingElements",
"value": "CredManCollectionGroupTable"
}
]
}
],
"hasAttributes": [
{
"name": "CollectionGroupId",
"dataType": "CredManCollectionGroupId",
"isNullable": true,
"displayName": "Collection group",
"maximumLength": 10
},
{
"name": "Description",
"dataType": "Description",
"isNullable": true,
"displayName": "Description",
"maximumLength": 60
},
{
"name": "CredmanCollectionGroupTableRecId",
"dataType": "other",
"isNullable": true,
"displayName": "Record-ID"
},
{
"name": "CredmanCollectionGroupTableDataAreaId",
"dataType": "other",
"isNullable": true,
"displayName": "Company"
}
],
"displayName": "MET Collection groups (Shared)"
},
{
"explanation": "Collection group",
"dataTypeName": "CredManCollectionGroupId",
"extendsDataType": "SysGroup"
},
{
"explanation": "Group",
"dataTypeName": "SysGroup",
"extendsDataType": "string"
},
{
"explanation": "Description",
"dataTypeName": "Description",
"extendsDataType": "string"
}
]
}
I need to find sqlViewDefinition from this file.
So far I can read the JSON in a JSON object. But could not find a way to get the view definition.
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Nancy.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public class FindFiles
{
// Main Method with int return type
static int Main(String[] args)
{
Console.WriteLine("Buid SQL");
// for successful execution of code
return X("FILE_NAME");
}
public static int X(string fileName)
{
//connection string
string storageAccount_connectionString = "CONNECTION_STRING";
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageAccount_connectionString);
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("CONTAINER");
//The specified container does not exist
try
{
//root directory
CloudBlobDirectory dira = container.GetDirectoryReference(string.Empty);
//true for all sub directories else false
var rootDirFolders = dira.ListBlobsSegmentedAsync(true, BlobListingDetails.Metadata, null, null, null, null).Result;
foreach (var blob in rootDirFolders.Results)
{
if (blob.Uri.OriginalString.Contains(fileName, StringComparison.OrdinalIgnoreCase) && blob.Uri.OriginalString.Contains(".cdm.json", StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine("Blob: " + blob.Uri.OriginalString);
if (blob.GetType() == typeof(CloudBlockBlob))
{
CloudBlockBlob b = (CloudBlockBlob)blob;
string jsonText = b.DownloadTextAsync().Result;
Dictionary<string, object> json_Dictionary = (new JavaScriptSerializer()).Deserialize<Dictionary<string, object>>(jsonText.ToString());
JObject json = JObject.Parse(jsonText);
}
}
}
}
catch (Exception e)
{
// Block of code to handle errors
Console.WriteLine("Error", e);
}
return 1;
}
}
As you are using .NET 6 and because the structure is always the same, the easiest way to deserialize is to mimic the structure of the JSON in C# classes. You can then easily deserialize the JSON into objects and access the properties of the objects instead of "brachiating" through dynamic data.
In order to get the classes, you can use Visual Studio's Paste Special function (Edit -> Paste special -> Paste JSON as classes). This generates the classes for you (you can adjust the classes if you don't need parts of them or change the casing of the property names; also you can use attributes to customize the serialization).
Afterwards, it is easy to parse the JSON into an object, e.g. (I've put your sample JSON into the jsonContent variable):
var obj = System.Text.Json.JsonSerializer.Deserialize<Rootobject>(jsonContent);
Because it still is a complex structure, getting to the SQL needs a bit of code:
Console.WriteLine(obj
.definitions[0]
.exhibitsTraits
.Where(x => x.traitReference == "has.sqlViewDefinition")
.First().arguments.Where(x => x.name == "sqlViewDefinition")
.First().value);
Finally, the above code writes the following output:
CREATE VIEW [DBO].[METCREDMANCOLLECTIONGROUPENTITY] AS SELECT T1.COLLECTIONGROUPID AS COLLECTIONGROUPID, T1.DESCRIPTION AS DESCRIPTION, T1.RECID AS CREDMANCOLLECTIONGROUPTABLERECID, T1.DATAAREAID AS CREDMANCOLLECTIONGROUPTABLEDATAAREAID, T1.RECVERSION AS RECVERSION, T1.PARTITION AS PARTITION, T1.RECID AS RECID FROM CREDMANCOLLECTIONGROUPTABLE T1
You can use this documentation to get familiar with JSON handling in .NET 6.

Fetch Value from Json without DeserializeObject C#

Following Json is return from API call.
{
"version": "dddf2222",
"data": {
"account": {
"username": "34343",
},
},
"error": 0
}
I want to fetch value of username without DeserializeObject.
dynamic stuff = JsonConvert.DeserializeObject(myJson);
Console.WriteLine(stuff.data.account.username);
using using Newtonsoft.Json.Linq
dynamic stuff = JObject.Parse(myJson);
Console.WriteLine(stuff.data.account.username);

Capture value from the JSON array

I am trying to call an API and getting the results back in the form of JSON. I need to parse the data received and collect the specific data "key" under mapping in below mentioned JSON data from the array. I also want to get the total count of key under the mapping, so that I can recurse the array and retrieve the key.
JSON data as below,
{
"$type": "Type1",
"mapping": [
{
"value": "Value1",
"key": "Key1"
},
{
"value": "Value2",
"key": "Key2"
}
]
}
I am stuck in the code until below mentioned. Was not sure how to proceed further. Kindly suggest how can I get the value
using (var webResponse = (HttpWebResponse)webrequest.GetResponse())
{
using (var sr = new StreamReader(webResponse.GetResponseStream()))
{
text = sr.ReadToEnd();
dynamic jsonObject = JsonConvert.DeserializeObject(text);
//Need suggestion how can I retrieve the specific
}
}
Here is a quick Console app that does what you are trying to do. The trick is to traverse the dynamic object until you find what you want. But cast everything as dynamic as you go.
class Program
{
static void Main(string[] args)
{
var text = "{ \"$type\": \"Type1\",\"mapping\": [ { \"value\": \"Value1\", \"key\": \"Key1\" }, { \"value\": \"Value2\", \"key\": \"Key2\" } ] }";
dynamic result = JsonConvert.DeserializeObject(text);
dynamic mapping = result.mapping;
foreach(dynamic item in mapping as IEnumerable<dynamic>)
{
Console.WriteLine("{0}: {1}", (string)item.value, (string)item.key);
}
var done = Console.ReadLine();
}
}

Pass username and password into json url

The URL Is:
http://reportguru.webdenza.com/vdetect-pro-2/api.php?q={\"svc\":\"auth\",\"params\":{\"username\":\"username\",\"password\":\"passowrd\"}}
The username and password should come from textboxes.Asp.net(c#) code is needed.
After passing the credentials the following json will come.
{ "items":
{ "642163":
{ "id": 642163,
"nm": "AK-21699-11-Lancer-Mohammed Al Noman" },
{ "642169":
{ "id": 642169,
"nm": "AK-21699-11-Lancer-Mohammed Al Noman" } ,
{ "642063":
{ "id": 642063,
"nm": "AK-21699-11-Lancer-Mohammed Al Noman" }
},
"sid": "fdf47003cc1eca9133822ba0025c6aea",
"count": 12,
"p_type": "hst"
}
fdf47003cc1eca9133822ba0025c6aea
All the items should come.I have 642163 like id 100.How get all these values in asp.net(c#).
Use System.Web.Script.Serialization.JavaScriptSerializer and it's Deserialize method to get a strong typed access to the JSON response.
If you don't mind adding new references, you could also have a look at Json.NET

How to parse JSON with dynamic variables in C#

I've tried multiple ways of doing this and can't seem to find the proper solution. The JSON I am trying to parse looks like this
{
"data":
{
"random1":
{
"language": "en",
"state": "fl"
},
"completelyrandom":
{
"language": "fr",
"state": "wa"
}
}
}
Currently I am using the below JSON.NET to deserialize into a dynamic object, which gives me access to "language", "state" but I don't know what the parent object is.
var jsonSerializer = new JsonSerializer();
dynamic value = jsonSerializer.Deserialize(new JsonTextReader(new StringReader(json)));
foreach (var obj in value.data)
{
var myObj = obj.First;
string language = myObj.language;
}
How do I get access to "random1" and "completelyrandom"
Yeah, writing all of it out cleared my head. obj.Name gives my the container.

Categories