I'm having trouble to properly iterate with the json response i got. There's no specific id/key to loop them.
First here is my sample json object.
The key's of json is id and is dynamic.
Sample JSON
{
"1": {
"docid": "26",
"title": "QT1030-3 Product Development Plan Template",
"category": "Template/Form",
"children": {}
},
"2": {
"docid": "27",
"title": "QT1030-2 Product Requirements Template",
"category": "Template/Form",
"children": {}
},
"3": {
"docid": "28",
"title": "QT1030-1 Product Specifications Template",
"category": "Template/Form",
"children": {}
},
"60": {
"docid": "22",
"title": "QT1030-7 Design & Development Review Template",
"category": "Template/Form",
"children": {}
},
"61": {
"docid": "23",
"title": "QT1030-6 Design Inputs Outputs Matrix",
"category": "Template/Form",
"children": {}
},
"63": {
"docid": "24",
"title": "QT1030-5 DHF Index Template",
"category": "Template/Form",
"children": {}
},
"82": {
"docid": "25",
"title": "QT1030-4 Material Specifications",
"category": "Template/Form",
"children": {}
}
Here i tried to loop dynamic but not sure how to iterate properly.
I tried something like item.docid but no success.
C# code
C# Code
if (response.IsSuccessStatusCode)
{
var documents = await response.Content.ReadAsStringAsync();
dynamic nodelist = JsonConvert.DeserializeObject(documents);
foreach (var item in nodelist)
{
int i = 0;
}
}
You are almost there, you can iterate it and get value of docid as follow:
dynamic nodeList= JsonConvert.DeserializeObject(documents);
foreach (JProperty node in nodeList)
{
Console.WriteLine(node.Value["docid"]);
}
Related
I am wrting a Function ARM template for CI/CD the function is already hosted on the Azure portal Now I decided to create an ARM function template and am getting the below error. It saying I cannot create a resource which already exists. I know the resource already exists but I want to create a CI/CD pipline based on the template. I have tried the incremental mode but it seems I am missing something. Is there any guidance online? I have taken the Azure Function Template from the AZURE Git.
C:\Program Files\Microsoft SDKs\Azure.NET SDK\v2.9> az group deployment validate --mode Incremental --resource-group cloud-shell-storage-southeastasia --template-file azuredeploy.json
Please provide string value for 'appName' (? for help): SchedulerHttpFunctionSample
azuredeploy.json
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"appName": {
"type": "string",
"metadata": {
"description": "The name of the function app that you wish to create."
}
},
"storageAccountType": {
"type": "string",
"defaultValue": "Standard_LRS",
"allowedValues": [
"Standard_LRS",
"Standard_GRS",
"Standard_RAGRS"
],
"metadata": {
"description": "Storage Account type"
}
},
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]",
"metadata": {
"description": "Location for all resources."
}
},
"runtime": {
"type": "string",
"defaultValue": "node",
"allowedValues": [
"node",
"dotnet",
"java"
],
"metadata": {
"description": "The language worker runtime to load in the function app."
}
}
},
"variables": {
"functionAppName": "[parameters('appName')]",
"hostingPlanName": "[parameters('appName')]",
"applicationInsightsName": "[parameters('appName')]",
"storageAccountName": "[concat(uniquestring(resourceGroup().id), 'azfunctions')]",
"storageAccountid": "[concat(resourceGroup().id,'/providers/','Microsoft.Storage/storageAccounts/', variables('storageAccountName'))]",
"functionWorkerRuntime": "[parameters('runtime')]"
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"name": "[variables('storageAccountName')]",
"apiVersion": "2018-12-01",
"location": "[parameters('location')]",
"kind": "Storage",
"sku": {
"name": "[parameters('storageAccountType')]"
},
"properties":{
"mode":"Incremental"
}
},
{
"type": "Microsoft.Web/serverfarms",
"apiVersion": "2018-02-01",
"name": "[variables('hostingPlanName')]",
"location": "[parameters('location')]",
"sku": {
"name": "Y1",
"tier": "Dynamic"
},
"properties": {
"mode":"Incremental",
"name": "[variables('hostingPlanName')]",
"computeMode": "Dynamic"
}
},
{
"apiVersion": "2018-02-01",
"type": "Microsoft.Web/sites",
"name": "[variables('functionAppName')]",
"location": "[parameters('location')]",
"kind": "functionapp",
"dependsOn": [
"[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]",
"[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]"
],
"properties": {
"serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]",
"mode":"Incremental",
"siteConfig": {
"appSettings": [
{
"name": "AzureWebJobsStorage",
"value": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('storageAccountName'), ';AccountKey=', listKeys(variables('storageAccountid'),'2015-05-01-preview').key1)]"
},
{
"name": "WEBSITE_CONTENTAZUREFILECONNECTIONSTRING",
"value": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('storageAccountName'), ';AccountKey=', listKeys(variables('storageAccountid'),'2015-05-01-preview').key1)]"
},
{
"name": "WEBSITE_CONTENTSHARE",
"value": "[toLower(variables('functionAppName'))]"
},
{
"name": "FUNCTIONS_EXTENSION_VERSION",
"value": "~2"
},
{
"name": "WEBSITE_NODE_DEFAULT_VERSION",
"value": "~10"
},
{
"name": "APPINSIGHTS_INSTRUMENTATIONKEY",
"value": "[reference(resourceId('microsoft.insights/components/', variables('applicationInsightsName')), '2015-05-01').InstrumentationKey]"
},
{
"name": "FUNCTIONS_WORKER_RUNTIME",
"value": "[variables('functionWorkerRuntime')]"
}
]
}
}
},
{
"apiVersion": "2018-02-01",
"name": "[variables('applicationInsightsName')]",
"type": "microsoft.insights/components",
"location": "East US",
"tags": {
"[concat('hidden-link:', resourceGroup().id, '/providers/Microsoft.Web/sites/', variables('applicationInsightsName'))]": "Resource"
},
"properties": {
"ApplicationId": "[variables('applicationInsightsName')]",
"Request_Source": "IbizaWebAppExtensionCreate",
"mode":"Incremental"
}
}
]
}
Azure Error: InvalidResourceLocation
Message: The resource 'SchedulerHttpFunctionSample' already exists in location 'southcentralus' in resource group 'cloud-shell-storage-southeastasia'. A resource with the same name cannot be created in location 'southeastasia'. Please select a new resource name.
It's because that name is already taken. Function App name need to be globally unique.
I would suggest using a suffix. In the variables section create some variables:
"variables": {
"suffix": "[uniqueString(resourceGroup().id, resourceGroup().location)]",
"functionAppName": "[concat(parameters('appName'), variables('suffix'))]"
}
The function uniqueString will generate a unique string base on the information received. So if you redeploy in the same resource group and region the suffix will be the same.
Have a look to this post: http://www.frankysnotes.com/2019/05/how-to-make-your-deployment-successful.html or this video https://www.youtube.com/watch?v=dnb-f4C052w
I have a valid json schema which is as below
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "abcd",
"title": "test schema",
"description": "............",
"type": "object",
"properties": {
"a": {
...........
...........
},
"b": {
.........
........
.........
},
"c": {
...........
..........
},
"d": {
...........
..........
}
},
"anyOf": [
{
"type": "object",
"$ref": "#/properties/a",
"$ref": "#/properties/b"
},
{
"type": "object",
"$ref": "#/properties/c",
"$ref": "#/properties/d"
}
]
}
The schema above is stored in a file and I'm loading this for parsing which can be seen below
JSchema schema =
JSchema.Parse(File.ReadAllText(#"D:\Backups\testschema.json"));
So when I look at the output of schema it is as below
My Json Schema
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "abcd",
"title": "test schema",
"description": "............",
"type": "object",
"properties": {
"a": {
...........
...........
},
"b": {
.........
........
.........
},
"c": {
...........
..........
},
"d": {
...........
..........
}
},
"anyOf": [
{
"$ref": "#/properties/b"
},
{
"$ref": "#/properties/d"
}
]
}
I'm wondering why I'm getting only the last reference under the anyOf property
On parsing shouldn't the output be the same as that in the file?
Am I missing something?
My desired output under anyOf is
"anyOf": [
{
"type": "object",
"$ref": "#/properties/a",
"$ref": "#/properties/b"
},
{
"type": "object",
"$ref": "#/properties/c",
"$ref": "#/properties/d"
}
]
Any thoughts on how I can achieve my desired output?
In Json, each object can only have a particular key once. So within one object, you can only have one key with the name $ref. The Json that you posted above is invalid; it's up to the implementation what it does - ideally it should throw an error but in this case it looks like the second one is overwriting the first one.
Note that with a $ref, the other properties are ignored, so there is little point in using another keyword like type in addition to the $ref.
I'm not entirely sure, but it looks like what you're trying to achieve is to say that either properties "a" and "b" should be present, or properties "c" and "d" should be present.
You can achieve that by replacing the anyOf clause with:
"anyOf": [
{
"required": ["a", "b"]
},
{
"required": ["c", "d"]
}
]
Example:
"database": {
"UUIDTOKEN": {
"name": "test",
"yearsold": "20",
"userid": "100",
"uuid": "a2b45475-aebe-4103-9c01-43ea8754dfc0",
"email": "email#gmail.com"
}
},
How to add new value ? so, database = main json text. How to add uuid {
name,yearsold,userid,uuid,email
}
"database": {
"UUIDTOKEN": {
"name": "test",
"yearsold": "20",
"userid": "100",
"uuid": "a2b45475-aebe-4103-9c01-43ea8754dfc0",
"email": "email#gmail.com"
},
"UUIDTOKEN": {
"name": "stackoverflow",
"yearsold": "24",
"userid": "110",
"uuid": "uuid-aebe-4103-9c01-43ea8754dfc0",
"email": "stackoverflow#gmail.com"
},
},
Thanks for help.
An object (the "database" in your case) can't have two fields with the same name ("UUIDTOKEN"). If you need your "database" to contain a list of "UUIDTOKEN" objects, consider changing it to an array:
{
"database": [{
"name": "test",
"yearsold": "20",
"userid": "100",
"uuid": "a2b45475-aebe-4103-9c01-43ea8754dfc0",
"email": "email#gmail.com"
}, {
"name": "stackoverflow",
"yearsold": "24",
"userid": "110",
"uuid": "uuid-aebe-4103-9c01-43ea8754dfc0",
"email": "stackoverflow#gmail.com"
}]
}
Please note, the "UUIDTOKEN" name was eliminated in this sample, but if you need it for some reason, add another object layer:
{
"database": [{
"UUIDTOKEN": {
"name": "test"
//...
}
}, {
"UUIDTOKEN": {
"name": "stackoverflow"
//...
}
}]
}
I'm not exactly sure what you are asking? Do you want to know how to add another value to a json object in c#, or do you want to know why what you've written won't work?
If it's the latter, you can't have a json object with 2 keys the same, you will need to change UUIDTOKEN to a unique key. Otherwise you could use an array.
I started testing OrientDB. I get the following JSON Response from the Demo Database:
{
"schema":{
"name":"OUser",
"properties":{
"roles":{
"name":"roles",
"linkedClass":"ORole",
"type":"LINKSET",
"mandatory":false,
"notNull":true,
"min":null,
"max":null
},
"name":{
"name":"name",
"type":"STRING",
"mandatory":true,
"notNull":false,
"min":null,
"max":null
},
"password":{
"name":"password",
"type":"STRING",
"mandatory":true,
"notNull":false,
"min":null,
"max":null
}
}
},
"result":[
{
"#type":"d",
"#rid":"#4:0",
"#version":0,
"#class":"OUser",
"name":"admin",
"password":"{SHA-256}8C6976E5B5410415BDE908BD4DEE15DFB167A9C873FC4BB8A81F6F2AB448A918",
"status":"ACTIVE",
"roles":[
"#3:0"
]
},
{
"#type":"d",
"#rid":"#4:1",
"#version":0,
"#class":"OUser",
"name":"reader",
"password":"{SHA-256}3D0941964AA3EBDCB00CCEF58B1BB399F9F898465E9886D5AEC7F31090A0FB30",
"status":"ACTIVE",
"roles":[
"#3:1"
]
},
{
"#type":"d",
"#rid":"#4:2",
"#version":0,
"#class":"OUser",
"name":"writer",
"password":"{SHA-256}B93006774CBDD4B299389A03AC3D88C3A76B460D538795BC12718011A909FBA5",
"status":"ACTIVE",
"roles":[
"#3:2"
]
}
]
}
How can you get a List of OUser Objects out of that? Using JSON.Net, JavaScriptSerializer or whatever?
There are a number of json parsers for c# at:
http://www.json.org/. It seems like fastJSON should be pretty quick.
Once logged in execute a query against OUser class:
select from ouser
Via HTTP protocol would be a GET request against this address:
http://localhost:2480/query/demo/sql/select%20from%20ouser
{
"result": [{
"#type": "d", "#rid": "#4:0", "#version": 0, "#class": "OUser",
"name": "admin",
"password": "{SHA-256}8C6976E5B5410415BDE908BD4DEE15DFB167A9C873FC4BB8A81F6F2AB448A918",
"status": "ACTIVE",
"roles": ["#3:0"]
}, {
"#type": "d", "#rid": "#4:1", "#version": 2, "#class": "OUser",
"name": "reader",
"password": "{SHA-256}3D0941964AA3EBDCB00CCEF58B1BB399F9F898465E9886D5AEC7F31090A0FB30",
"status": "ACTIVE",
"roles": ["#3:1"]
}, {
"#type": "d", "#rid": "#4:2", "#version": 0, "#class": "OUser",
"name": "writer",
"password": "{SHA-256}B93006774CBDD4B299389A03AC3D88C3A76B460D538795BC12718011A909FBA5",
"status": "ACTIVE",
"roles": ["#3:2"]
}
]
}
this looks like an easy structure to deserialise using json.NET
I've successfully created code that serializes a complex javascript object and posts it to an ASP.NET page. I can get the JSON string using Request.Form and the result looks like this (I've added indentation for readability):
{
"gasterPerStolPerVecka": {
"name": "gasterPerStolPerVecka",
"keyValue_id": "2",
"set_id": "1",
"containerElement": "#gasterPerStolPerVecka",
"keyValueComponents": [
{
"name": "gasterPerStolPerVecka_guestsPerWeek",
"value": "200"
},
{
"name": "gasterPerStolPerVecka_numberOfChairs",
"value": "100"
}
],
"keyValueUnitText": "gäster/stol per vecka",
"keyValueCalculationMethod": "divide",
"isValid": true,
"result": 2
},
"tillgangligaStolstimmarPerVecka": {
"name": "tillgangligaStolstimmarPerVecka",
"keyValue_id": "1",
"set_id": "1",
"containerElement": "#tillgangligaStolstimmarPerVecka",
"keyValueComponents": [
{
"name": "tillgangligaStolstimmarPerVecka_openHoursPerWeek",
"value": "35"
},
{
"name": "tillgangligaStolstimmarPerVecka_numberOfChairs",
"value": "100"
}
],
"keyValueUnitText": "stolstimmar/vecka",
"keyValueCalculationMethod": "multiply",
"isValid": true,
"result": 3500
},
"planeradIntaktPerTillgangligStolOchVecka": {
"name": "planeradIntaktPerTillgangligStolOchVecka",
"keyValue_id": "",
"set_id": "",
"containerElement": "#planeradIntaktPerTillgangligStolOchVecka",
"keyValueComponents": [
{
"name": "planeradIntaktPerTillgangligStolOchVecka_weeklyRevenue",
"value": ""
},
{
"name": "planeradIntaktPerTillgangligStolOchVecka_numberOfChairs",
"value": "100"
}
],
"keyValueUnitText": "kr",
"keyValueCalculationMethod": "divide",
"isValid": false,
"result": null,
"errorText": "Ofullständigt underlag för beräkning."
},
"planeradIntaktPerTillgangligaStolstimmar": {
"name": "planeradIntaktPerTillgangligaStolstimmar",
"keyValue_id": "",
"set_id": "",
"containerElement": "#planeradIntaktPerTillgangligaStolstimmar",
"keyValueComponents": [
{
"name": "planeradIntaktPerTillgangligaStolstimmar_weeklyRevenue",
"value": ""
},
{
"name": "planeradIntaktPerTillgangligaStolstimmar_openHoursPerWeek",
"value": "35"
},
{
"name": "planeradIntaktPerTillgangligaStolstimmar_numberOfChairs",
"value": "100"
}
],
"keyValueUnitText": "kr",
"keyValueCalculationMethod": "divide_divide",
"isValid": false,
"result": null,
"errorText": "Ofullständigt underlag för beräkning."
}
}
Now I try to deserialize this on the server side, but it's difficult. I keep getting the error:
[NullReferenceException: Object reference not set to an instance of an object.]
I don't know where to start looking for errors?
Thanks in advance!
/Thomas Kahn
use stringify instead serialization
You need to use a deserialization library for ASP.NET. See http://json.org/ for libraries that are available or maybe there is one built into ASP.NET. Either way, the code will look like:
String s = getAppropriateFormField();
Object o = JSONLibraryPackage.parse(s);
where obviously you'll have to fill in the blanks for how you get the form field and then what package and method does the parsing.