JSONpath filter expression to return boolean - jsonpath

Problem:
I want to get boolean value true if the the Array features in the json contains test1
Example:
{
"features": [
"test1",
"test2",
"test3"
],
"name": "David"
}
For the above json, result should be true.

Related

Cosmos Nested JSON Query

This is the fist time that I work with CosmosDB and I am trying to create a Query to get some details about this JSON:
{
"Status": "Uploaded",
"ProvidedOn": "2022-04-04T18:34:57.4160484Z",
"DocumentTaxonomy": {
"JurisdictionalCountriesOfService": [
{
"Id": 5,
"Name": "United States"
}
],
"WorkProduct": {
"Id": 762,
"Name": "Other reports and documents",
"Type": "Info item"
}
},
"id": "3a92c052-bc23-4b8a-acbf-54044785968a",
"Meta": {
"VersionId": "3",
"LastUpdated": "0001-01-01T00:00:00",
"Source": null,
"Security": null,
"Tag": null,
"Id": null,
"Extension": null,
"ModifierExtension": null
},
}
Basicaly I need to get something like
SELECT id,Status,ProvidedOn, WorkProductName, WorkProductType,MetaVersionId FROM JSONFILE
In this image I am highlighting the columnsthat my query needs
NOTE: since I need to query different CosmoDB, not all of them have the DocumentTaxonomy section so the plan is when they doesn't exists return like a null or blank value
As per your question, the code should return the DocumentTaxonomy section values if they exist in the JSON otherwise It should return null or blank values.
This code may work for you:
SELECT c.id, c.ProvidedOn, c.Status,c.Meta.VersionId as versionId,
IS_DEFINED(c.DocumentTaxonomy.WorkProduct.Type) = true ? c.DocumentTaxonomy.WorkProduct.Type
: IS_DEFINED(c.DocumentTaxonomy.WorkProduct.Type) = false ? null
: "some default value"
as TypeDoc,
IS_DEFINED(c.DocumentTaxonomy.WorkProduct.Name) = true ? c.DocumentTaxonomy.WorkProduct.Name
: IS_DEFINED(c.DocumentTaxonomy.WorkProduct.Name) = false ? null
: "some default value"
as NameDoc
FROM c
The Output it gave when DocumentTaxonomy section exists is:
[
{
"id": "3a92c052-bc23-4b8a-acbf-54044785968a",
"ProvidedOn": "2022-04-04T18:34:57.4160484Z",
"Status": "Uploaded",
"versionId": "3",
"TypeDoc": "Info item",
"NameDoc": "Other reports and documents"
}
]
The Output when DocumentTaxonomy section not exists :
[
{
"id": "3a92c052-bc23-4b8a-acbf-54044785968a",
"ProvidedOn": "2022-04-04T18:34:57.4160484Z",
"Status": "Uploaded",
"versionId": "3",
"TypeDoc": null,
"NameDoc": null
}
]
Please check the screenshot of the output for your reference:

How to get the first value of a filtered JSONPath output

Below is the sample json that I used to get data based on string search using JSONPath.
{
"tool":
{
"jsonpath":
{
"creator":
{
"name": "Jayway Inc.",
"location":
[
"Malmo",
"San Francisco",
"Helsingborg"
]
}
}
},
"book":
[
{
"title": "Beginning JSON",
"price": 49.99
},
{
"title": "JSON at Work",
"price": 59.99
}
]
}
The JSONPath expression used is
"$.book[?(#.price == 49.99)].title"
The response for the above JSONPath is an array with single string that is the title.
[
"Beginning JSON"
]
Is it possible to get the output as a String instead of an array. It is ok to get the first value if it contains multiple values in the array.

JSONPatch WCF REST API getting invalid payload error

I am trying to use JSONPatch(KevinDockx version) in my WCF REST API(ASP.NET v4.5). My operation contract is as below:-
[OperationContract]
[WebInvoke(UriTemplate = "/{raceId}/participants", Method = "PATCH")]
void UpdateRace(string id, JsonPatchDocument<ParticipantContract[]> participantsContract);
And implementation as follows :-
public void UpdateRace(string id, JsonPatchDocument<ParticipantContract[]> participantsContract)
{
//Someoperation
}
My data is like the below format where I want to perform add, update delete, move and swap operations on the participants array.
{
"raceId" : 1
"participants": [
{
"id": "abc",
"car": "Mercedes",
"model": "F1 W08 EQ Power",
"teamname": "Mercedes-AMG Petronas Motorsport",
"driver": {
"id": "111",
"firstname": "Lewis",
"lastname": "Hamilton",
"age": "29"
},
"codriver": {
"id": "222",
"firstname": "Valtteri",
"lastname": "Bottas",
"age": "32"
}
},
{
"id": "def",
"car": "Ferrari",
"model": "SF70H",
"teamname": "Scuderia Ferrari",
"borrower": {
"id": "333",
"firstname": "Sebastian",
"lastname": "Vettel",
"age": "30"
},
"coborrower": {
"id": "444",
"firstname": "Kimi",
"lastname": "Räikkönen",
"age": "37"
}
}
]
}
On JSON deserialization I am getting below error:-
{
"summary": "Bad Request",
"details": "Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'ParticipantContract' because the type requires a JSON object (e.g. {\"name\":\"value\"}) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON object (e.g. {\"name\":\"value\"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.\r\nPath '', line 1, position 1."
}
Can you help me with what I am missing in this? Is there something additional that needs to be done?
As I can see your JSON object is not valid
{
"raceId" : 1 // you missed comma here
"participants": [
{
Just checking for any other mistake in your JSON object and passing a valid JSON will fix the error.

How can I write a document db query which returns results of sub-documents satisifying any conditions of input list?

Let's assume that I have the following document:
[
{
"name" : "obj1",
"field": [ "Foo1", "Foo3" ]
},
{
"name": "obj2",
"field": [ "Foo2" ]
},
{
"name": "obj3",
"field": [ "Foo3" ]
},
{
"name": "obj4",
"field": [ "Foo1" ]
}
]
I want to write a query which returns obj1, obj3, and obj4 when field = "Foo1" or "Foo3" are searched for. Obviously I can write something like:
SELECT * FROM c WHERE ARRAY_CONTAINS(c.field, "Foo1") OR ARRAY_CONTAINS(c.field, "Foo3")
Though I want to avoid constructing a long query by concatenating query string with ARRAY_CONTAINS for each value in search list.
How can this query be expressed succinctly?
You could rewrite the query using JOIN as follows:
SELECT c
FROM c
JOIN tag IN c.field
WHERE ARRAY_CONTAINS(["Foo1", "Foo3"], tag)
Note that if you have an object with both tags, then it would occur multiple times in the result, and you have to perform distinct/de-duping on the client side.

I have 2 documents, how can I write a query which return output as single document

Document 1
{
"accountId": "Test",
"documentType": "config1",
"name": "TestName",
"url": "http://foo"
"date": "2015-08-26T20:27:10.7204029Z"
}
Document 2
{
"accountId": "Test",
"documentType": "config2",
"area": "testArea",
}
Expected output as single document:
{
"accountId": "Test",
"documentType": "config",
"name": "TestName",
"area": "testArea"
}
In short, you can't.
They are two documents. You can't JOIN two independent units together.
You can have a single document with embedded entities, then it is a single document with two sub-documents and you could potentially do something like this.
So maybe have {"accountId" : "Test", "config" : [ {"id": "config1", "" }, {"id" : "config2" } ] }

Resources