Wildcard value in JSONPath - c#

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"));

Related

GraphQL property names first letter

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 ?

Pact Net, Two different body structure comparison in Contract Test Is possible using pact?

I have a question in Pact. I have two body structure, how to compare this using pact dotnet? I see .PactVerify("url") simply compare the entire body of contract json file and it fails.
consumer
{
"name":"xxx1",
"isemployed":"yes",
"country":"USA"
}
provider:
[
{
"type": "work",
"employee": [
{
"name": "xxx1",
"isemployed": "yes",
"country": "USA"
},
{
"name": "xxx2",
"isemployed": "yes",
"country": "india"
}
]
}
]

JSON extract date from matching string

I am entirely new to JSON, and haven't got any familiarity with it at all. I'm tinkering around with some JSON data extracts to get a feel for it.
Currently, I have a chat export which has a large number of keys. Within these keys are a "date" key, and a "from_id" key.
I would like to search a JSON file for a matching value on the "from_id" key, and return all the values against the "date" keys with a matching "from_id" value.
For example:
{
"name": "FooBar Chat Group",
"type": "textchat",
"id": 123456789,
"messages": [
{
{
"id": 252930,
"type": "message",
"date": "2021-03-03T01:39:30",
"date_unixtime": "1614735570",
"from": "Person1",
"from_id": "user1234",
"text": "This is a message!"
},
{
"id": 252931,
"type": "message",
"date": "2021-03-03T01:41:03",
"date_unixtime": "1614735663",
"from": "Person2",
"from_id": "user9876",
"text": "This is a reply!"
},
{
"id": 252932,
"type": "message",
"date": "2021-03-03T01:42:01",
"date_unixtime": "1614735721",
"from": "Person2",
"from_id": "user9876",
"text": "This is some other text!"
},
{
"id": 252933,
"type": "message",
"date": "2021-03-03T01:42:44",
"date_unixtime": "1614735764",
"from": "Person1",
"from_id": "user1234",
"text": "Yeah, indeed it is!"
}
]
}
I want to search from_id "user1234", and for it to return the following:
2021-03-03T01:39:30
2021-03-03T01:42:44
These are the two dates that have a matching from_id.
How would I go about doing something like this, please?
I am entirely new to this, so a super basic explanation with resources would really be appreciated. Thanks!
you can try this c# code. At first you have to parse your json strig to create an object from string. Then you can use LINQ to get the data you need
using Newtonsoft.Json;
JArray messages = (JArray) JObject.Parse(json)["messages"];
string from_id="user1234";
DateTime[] dates = messages
.Where(m=> (string) m["from_id"] ==from_id)
.Select(m => (DateTime) m["date"])
.ToArray();
Assuming your sample is part of a JSON Array - starts with [ and ends with ] - you should be able to iterate and conditionally select what you want.
In javascript, you can use a for of to iterate through and an if to match your request:
for(let item of array){
if(item['from_id'] == 'user1234')
console.log(item.date);
}
As we don't know the language you're actually using, a more wide code version of it could be something like:
for(let i=0;i < array.length; i++){
if(array[i]['from_id'] == 'user1234'){
print(array[i]['date']);
}
}

Solr .Net Facet Term for obtaining numBuckets

Trying to implement with the following Facet Term which works when querying through postman but I cannot find any equivalent in Solr.NET as there are not facet term queries. I can see by field, range, and pivots. I would need to get the numBuckets and I do not see how this can me achieved in with the Solr.NET libraries.
Postman body example.
{
"query": "*:*",
"fields": [
"user_id"
],
"filter": [
"",
],
"facet": {
"user_id": {
"mincount": 1,
"numBuckets": true,
"allBuckets": false,
"offset": 0,
"type": "terms",
"field": "user_id",
"limit": 1
}
},
"params": {
"echoParams": "none"
},
"limit": 100,
"offset": 0,
"sort": "ismail_bius asc,createdon_zius desc"
}
I have tried to pass it in the ExtraParams property of the query options but no luck on it.
Thanks.

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