GraphQL property names first letter - c#

Im using C# to write GraphQL queries and execute them
i created the schema and everything works fine i get the data back exactly as i want,
but the first letter of the name of the properties gets changed from a capital letter to a small letter.
this is my schema code,
var schema = Schema.For(#"
type user {
Indx: Int,
UserName: String,
Password: String
}
type Query {
users: [user]
}
",s=>s.Types.Include<Query>());
this is the execution code, this execution method works fine returns all the data i want from my tables but instead of returning property names with first letter capitalized it returns property names with first letter small.
var json = schema.ExecuteAsync( s => { s.Query = "{ users {indx,userName,password} }"; });
this is the result I get:
{
"data": {
"users": [
{
"indx": 593,
"userName": "string",
"password": "string"
},
{
"indx": 540,
"userName": "new test user",
"password": "1234"
},
{
"indx": 598,
"userName": "wolv",
"password": "1234"
}
]
}
}
if i change the execution to as follows :
var json = schema.ExecuteAsync( s => { s.Query = "{ users {Indx,UserName,Password} }"; });
i get an error in return:
{
"errors": [
{
"message": "Cannot query field \u0027Indx\u0027 on type \u0027user\u0027. Did you mean \u0027indx\u0027?",
"locations": [
{
"line": 1,
"column": 10
}
],
"extensions": {
"code": "FIELDS_ON_CORRECT_TYPE",
"codes": [
"FIELDS_ON_CORRECT_TYPE"
],
"number": "5.3.1"
}
},
{
"message": "Cannot query field \u0027UserName\u0027 on type \u0027user\u0027. Did you mean \u0027userName\u0027?",
"locations": [
{
"line": 1,
"column": 15
}
],
"extensions": {
"code": "FIELDS_ON_CORRECT_TYPE",
"codes": [
"FIELDS_ON_CORRECT_TYPE"
],
"number": "5.3.1"
}
},
{
"message": "Cannot query field \u0027Password\u0027 on type \u0027user\u0027. Did you mean \u0027password\u0027?",
"locations": [
{
"line": 1,
"column": 24
}
],
"extensions": {
"code": "FIELDS_ON_CORRECT_TYPE",
"codes": [
"FIELDS_ON_CORRECT_TYPE"
],
"number": "5.3.1"
}
}
]
}
I want the data to be returned like this:
{
"data": {
"users": [
{
"Indx": 593,
"UserName": "string",
"Password": "string"
},
{
"Indx": 540,
"UserName": "new test user",
"Password": "1234"
},
{
"Indx": 598,
"UserName": "wolv",
"Password": "1234"
}
]
}
}
any ideas ?

Related

Extract Items in Nested Json Array

How can I extract items from nested Json Array using Newtonsoft.Json functions or methods? I have a Json like this
{
"Bounds": {
"TextLength": 1379
},
"DocumentTypeName": "Invoice",
"DocumentTypeField": {
"Value": "Invoice",
"Confidence": 1
},
"Fields": [
{
"FieldId": "RPA.DocumentUnderstanding.Invoice.LineItems",
"FieldName": "Line Items",
"Values": [
{
"Components": [
{
"FieldId": "RPA.DocumentUnderstanding.Invoice.LineItems.Body",
"FieldName": "Body",
"Values": [
{
"Components": [
{
"FieldId": "RPA.DocumentUnderstanding.Invoice.LineItems.Item",
"FieldName": "Item",
"Values": [
{
"Components": [],
"Value": "Film 4C for the publication \"Racing World\" Visual: PNSP 02 05 Ref. 2004/021 Graphic designer honoraries 560010",
"Confidence": 0.962736368
}
]
},
{
"FieldId": "RPA.DocumentUnderstanding.Invoice.LineItems.UnitPrice",
"FieldName": "Unit Price",
"Values": [
{
"Components": [],
"Value": "400.00",
"Confidence": 0.9779528
}
]
}
],
"Confidence": 0.9432406
}]}],
"Confidence": 0.920952857}]}]}
and I want to extract the red highlighted fields from it.
Any help will be much appreciated.
Thanks
Since not deserializing is not a requirement you can do that. Just create C# object that has the exact same structure as your JSON and then do
var yourObject = JsonConvert.DeserializeObject<YourCSharpClassHere>(yourJsonString);
Then it's just a simple matter of getting the values
var fieldName = yourObject.Values[0].Components[0].Values[0].Components[0].FieldName
You can use JSON Query
Example
var fieldNames = o.SelectTokens("Values[*].Components[*].Values[*].Components[*].FieldName");

Query Azure DocumentDB that has two attributes with the same name

I'm trying to query DocumentDB by the following attribute
"Loop": [
{
"#LoopId": "0204",
"#Name": "Order Information Detail",
"OID": {
"OID01": null,
"OID02": "1NN***"
}
},
{
"#LoopId": "0210",
"#Name": "Quantity Information",
"QTY": {
"#comment": [],
"QTY01": {
"#text": "H9"
},
"QTY02": "10",
"QTY03": null,
"QTY04": "Pallets"
}]
using the Query
SELECT * FROM c WHERE c.Loop[0].OID.OID02="1NN***";
however the same document has another attribute Loop in it:
"Loop": [
{
"#LoopId": "0100",
"#Name": "Party Identification",
"N1": {
"#comment": [],
"N101": {
"#text": "CA"
},
"N102": "BN*********",
"N103": {
"#text": "2"
},
"N104": "B***"
}
},
{
"#LoopId": "0100",
"#Name": "Party Identification",
"N1": {
"#comment": [],
"N101": {
"#text": "SF"
},
"N102": "*****"
}]
is there anyway around this because I need to get the information by OID
thanks in advance
Can you try doing this query, as I think each "row" in the Loop array is a different Loop object:
SELECT * FROM Loop c WHERE c.OID.OID02 = "1NN***";

Wildcard value in JSONPath

I have the below JSON that I am trying to interpret, using json.net.
{
"platformUpdateDomain": 0,
"platformFaultDomain": 0,
"vmAgent": {
"vmAgentVersion": "2.5.1198.709",
"statuses": [
{
"code": "ProvisioningState/succeeded",
"level": "Info",
"displayStatus": "Ready",
"message": "GuestAgent is running and accepting new configurations.",
"time": "2015-04-21T11:42:44-07:00"
}
]
},
"disks": [
{
"name": "myosdisk",
"statuses": [
{
"code": "ProvisioningState/succeeded",
"level": "Info",
"displayStatus": "Provisioning succeeded",
"time": "2015-04-10T12:44:10.4562812-07:00"
}
]
}
],
"statuses": [
{
"code": "ProvisioningState/succeeded",
"level": "Info",
"displayStatus": "Provisioning succeeded",
"time": "2015-04-10T12:50:09.0031588-07:00"
},
{
"code": "PowerState/running",
"level": "Info",
"displayStatus": "VM running"
}
]
}
I wish to extract the Status, where the code contains the below value:
PowerState
However, I cannot work out how to do this, I can match against the entire string using Json.Net, but I'd like to
myJsonJObject.SelectToken("$.statuses[?(#.code == 'PowerState/running')]");
However, part of the "code" value can change, so I'd like to try and search based on the below condition
myJsonJObject.SelectToken("$.statuses[?(#.code == 'PowerState/*')]");
What is the correct Jsonpath expression to do this? Matching against a substring would also work, but again, I cannot find an example to do this.
Workaround is to use linq.
select all the status objects, filter on the code value, which has been converted to a string to use the contains method.
var x = myJsonJObject.SelectToken("$.statuses").Where(y => ((string)y.SelectToken("$.code")).Contains("PowerState"));

how to make json object in c#?

I have all values from aspx page. now I want to prepare following json values by picking up from the control values of aspx where user has inputted and want to submit to other application in same format.For that below is the example I want to make exact copy as like below.
var tempk = {
"requestTypeCode": "PRE_DETERMINATION",
"billingProvider": {
"npi": "1234567893",
"ein": "111222333",
"payerAssignedProviderId": "XYZ321"
},
"patient": {
"relationshipCode": "01",
"lastName": "Smith",
"firstName": "Bob",
"stateCode": "FL",
"birthDate": "1980-02-12",
"genderCode": "M"
},
"payer": {
"id": "BCBSF"
},
"submitter": {
"id": "123456789",
"lastName": "SUBMITTER"
},
"subscriber": {
"memberId": "JDH001",
"groupName": "ASDF 1-2",
"groupNumber": "12312412"
},
"claimInformation": {
"placeOfServiceCode": "11",
"diagnoses": [
{
"qualifierCode": "ABK",
"code": "J3089"
}
],
"serviceLines": [
{
"procedureCode": "92523",
"quantity": "100",
"amount": "250",
"fromDate": "2016-05-10"
}
]
}
}
Can you assist me how with the C# code will achieve?
You can use Newtonsoft.Json from nuget
http://www.newtonsoft.com/json

Deserialize Dynamic Json string using Newtonsoft JSON.NET

I have a JSON string that I'm getting from Facebook API, in which I have a node whose name changes according to its content, for example some time it is 45, or 58 etc.
It could be any number.
I want its value. How to get it?
Example:
{
"data": [
{
"id": "1492292372_10201810786059989",
"created_time": "2014-04-05T09:00:54+0000"
},
{
"id": "1492292372_10201804679827337",
"created_time": "2014-04-04T07:29:07+0000"
},
{
"id": "1492292372_10201804649306574",
"created_time": "2014-04-04T07:10:33+0000"
},
{
"id": "1492292372_10201801316823264",
"created_time": "2014-04-03T18:31:50+0000"
},
{
"id": "1492292372_10201798962284402",
"created_time": "2014-04-03T06:24:47+0000"
},
{
"message_tags": {
"0": [
{
"id": "1492292372",
"name": "Yawar Sohail",
"type": "user",
"offset": 0,
"length": 12
}
],
"15": [
{
"id": "1489845168",
"name": "Zeeshan Anjum",
"type": "user",
"offset": 15,
"length": 13
}
]
},
"id": "1492292372_10201796274777216",
"created_time": "2014-04-02T17:57:05+0000"
},
{
"id": "1492292372_10201794080482360",
"created_time": "2014-04-02T07:26:23+0000"
},
Inside message_tags there are two nodes [0 and 15] they dynamically changes according to their offset values. I want names, type and ids inside these nodes.
You can deserialize your JSON into an ExpandoObject:
var converter = new ExpandoObjectConverter();
dynamic obj = JsonConvert.DeserializeObject<ExpandoObject>(json, converter);
Which dynamically adds members to your object at runtime, and allows you to iterate over them as described in this answer:
foreach (var prop in obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
Console.WriteLine("Name: {0}, Value: {1}",prop.Name, prop.GetValue(obj,null));
}
That way you can iterate over obj.message_tags to get the individual messages, and obtain all their details respectively.

Categories

Resources