How to exclude fields from selection with jsonPath - json-path-expression

I am trying to exclude some fields from Json by jsonPath expression but it doesnt works. I am not able to write properly expression that match just some fields, without excluded.
I have tried to use somethink like this $.personal.[?(!#.email)] but it not works.
I have json like this:
{
"personal": {
"fullName": "full Name",
"firstName": "first Name",
"email": "hello#email.com"
}
}
I tried to use this line to select fields without email field:
$.personal.[?(!#.email)]
But i got only this results or false:
[
"full Name",
"first Name",
"hello#email.com"
]
I would like to have results like this:
[
"full Name",
"first Name"
]

As far as I know, you cannot exclude a property (ie. a blacklist approach), but you can select which properties to include (ie. a whitelist approach).
In this second case, any properties you leave out are not captured by the query.
So in your example,
$.personal.['fullname','firstname']
is what you need. Its not quite the same thing because you may not know the property names ahead of time

Related

How do I get a group count with multiple columns in the group with a virtual attribute

Keeping is simple assume I have a model "Loan" that has 2 attributes "Type" and "Status". Type as possible values of "Home", "Auto", "Building" and Status has possible values of "Open", "Pending", "Closed" and nil.
Without manually writing the SQL (using group and count?) how do I get the results of:
select type, status, count(*) from loans group by 1, 2;
I could do
Load.group(:type, :status).count()
But there are situations where status might be nil. In that case I want to replace the nil with "Unknown"
I tried adding a virtual attribute to the model:
def usable_status
status.nil? ? 'Unknown' : status
end
So I could then do
Load.group(:type, :usable_status).count()
But count didn't seem to recognize the virtual attribute since it is trying to pass it directly to the database.
Ideas?
Well, that's pretty easy to achieve by combining the rails styling with the SQL functionality:
Loan.group(:type, "COALESCE(status, 'Unknown')").count()

Create SharePoint list item with lookup field with Microsoft Graph

I am trying to create list items with Microsoft Graph's new SharePoint endpoint. My URL is as follows:
https://graph.microsoft.com/beta/sites/{site-id}/lists/{list-id}/items
Calling this URL with POST, and a body like this:
{
"fields": {
"Title": "test",
}
}
.. works! But if I include a lookup field, the lookup field is always empty. I have tried with
"{columnName}": "id",
"{columnName}": "id;#value",
"{columnName}": {
"#odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedReference", //and others like this
"Id": "id",
"Value": "value",
}
"{columnName}": "value",
"{columnName}Id": "id",
None of these seems to work. Some give an error like "The request is malformed or incorrect.", others go through, but doesn't include the lookup field.
The documentation for this is scarce (if any), and I have found very little information on google (apart from someone asking the same question with no answers). Anyone that got this to work yet? Is it even possible?
I have got this to work with people fields where the multiple selection option was DISABLED. So if you have a person field which allows one person only, the following works.
"{columnName}LookupId": id
Additionally, the id still needs to be the SP ID and not the graph user GUID. This ID changes from site to site.
You need to add a lookup column in the following format
"Office": "London",
"OfficeLookupId", "16"
Where Office is the name of the column

OData PUT to replace collection property

Using the OData standard is it possible to replace collection by sending a new collection?
Scenario:
The person object contains a list Address object. I would want to replace the Address collection with a new collection.
PUT Persons(1)/Addresses
[{"city": "X", "country": "US"}, {"city": "Y", "country": "US"}]
This is not possible out of the box (at least for ODATAv3), as the default routing template does not expect segments after the key portion.
But you should be able to add an ODATA Action that would do what you want to achieve. Your action definition could then look similar to this:
var action = builder.Entity<Person>()
.Action("Addresses")
.Returns<bool>();
action.Parameter<Collection<CityCountryPair>>("data");
The type CityCountryPair would be a regular DTO containing your properties you want to change. Make sure this type is also registered as an EntitySet in Odata or use a plain map/dictionary with only primitive types.
The actual call to the ODATA action would then look similar to this:
POST http://www.example.com/api/YourEndpoint/Persons(42)/Addresses
Content-Type: application/json
{
"data" :
[
{ "city" : "Berne" , "country": "CH" },
{ "city" : "Y" , "country": "CH" }
]
}
If you want to send more complex data types you can still resort to a customer JSON Deserialiser and override the default one or use a custom model binder after all.

Grails how to dynamically search based on varying search parameters

Let's say we have to perform a search on a bunch of database records and the search criteria is defined by a few check boxes and a text field. So, if check box for 'US Citizen' is checked and the textfield 'name' is filled with "John Doe". Then my search in my Grails application will be something like:
def results = IndividualRecord.findAllWhere(citizenship: 'US', name: 'John Doe')
now, if someone also checks the box 'voter' then I'll change my search to:
def results = IndividualRecord.findAllWhere(citizenship: US, name: 'John Doe', voter: true)
In reality my application has a couple of text fields and 6 checkboxes. Obviously I cannot write a custom search function based on each different combination of criteria there's just too many combinations. What would be an efficient way to tackle this problem? So, I guess ideally there would be one search function that could take in the customized query parameters and search with those. I'm a bit lost and confused. Any help appreciated.
In reality my application has a couple of text fields and 6 checkboxes. Obviously I cannot write a custom search function based on each different combination of criteria there's just too many combinations
You don't really need to concern yourself with handling the combinations. If you can receive the field names and values as a Map, you can simply pass the Map to findAllWhere, e.g.
def search(Map predicates) {
IndividualRecord.findAllWhere(predicates)
}
If for some reason you don't like this approach, you can use a criteria query instead, e.g.
def search(Map predicates) {
IndividualRecord.withCriteria {
if (predicates.voter != null) {
eq 'voter', predicates.voter
}
if (predicates.citizenship) {
eq 'citizenship', predicates.citizenship
}
if (predicates.name) {
eq 'name', predicates.name
}
}
}

Assign value from api response to specific model object in RAILS

I have a Contact model that has three attributes:
name, email, id
name and email are both present, but id is null.
I am using name and email to pass in through an API Post call, and this is the response I get back:
{"count": 1, "next": null, "previous": null, "results": [
{"id": 8067950, "name": "Bill","email": "test#test.com"}]}
My loop looks like this:
contacts_array.each do |contact|
api_call(contact.name, contact.email)
end
contacts_array is an array of ruby objects, namely Contact models. How can I, within that loop, assign the response id I get back to the contact used in the api_call?
I didn't really understand the difference between your Person and Contact model, but I'll assume they're the same model.
Generally you should know that it's a bad idea to handle the field called id on your own in rails. If you insist on storing the id which is returned from the call, then you should store it in another field, like api_id for example.
Below is a code that should work fine if api_call returns a Ruby Hash like the one in your question.
contacts_array.each do |contact|
contact.api_id = api_call(contact.name, contact.email)['results'].first['id']
end

Resources