Using JSONPath to filter and get only specific properties as result? - jsonpath

Is it possible to use JSONPath to both filter and "select" properties to keep for the result.
For instance if we have the following JSON:
{
"firstName": "John",
"lastName": "doe",
"age": 26,
"address": {
"streetAddress": "naist street",
"city": "Nara",
"postalCode": "630-0192"
},
"phoneNumbers": [
{
"type": "iPhone",
"number": "0123-4567-8888"
},
{
"type": "home",
"number": "0123-4567-8910"
}
]
}
And we run a JSONPath like:
$.phoneNumbers[?(#.type=='iPhone')]
We would end up with the result:
[
{
"type": "iPhone",
"number": "0123-4567-8888"
}
]
Now lets say that what I actually want is the phonenumber.. so we could do this:
$.phoneNumbers[?(#.type=='iPhone')].number
Which would result in:
[
"0123-4567-8888"
]
However.. the result that I would like to get would be:
[
{
"number": "0123-4567-8888"
}
]
Notice how the actual JSON-object is returned and the property..
Is that possible to achive somehow using JSONPath?

Since you know what property you're looking for (i.e. number), what you can do is to construct a new Json object with the property name and the returned value.
The actual implementation depends on what programming language you're using, but the pseudocode should look somewhat like this:
returnedList = json.find("$.phoneNumbers[?(#.type=='iPhone')].number")
initialize newJsonArray
foreach(returnedValue in returnedList):
newJsonArray.add(newJsonObject("number", returnedValue))
An alternative way to tackle the problem in OOP languages is to use lambda functions to filter your Json objects instead of using Jsonpath filters. An example on how to do it in C# using Newtonsoft.Json:
JArray jArray = new JArray(jObject.SelectToken("$.phoneNumbers").Where(x => x["type"].ToString() == "iPhone"));

Related

Using jq to get arrays where a key within the array is equal to a specific value?

I have been practicing with jq play to try to get all the arrays in a list where website is == "google" and create another json list from that.
https://jqplay.org/s/DKNC2mhOLq
jq: error (at :18): Cannot index array with string "website"
exit status 5
{
"items": [
{
"name":"name1",
"id":"1",
"website":"google"
},
{
"name":"name1",
"id":"1",
"website":"google"
},
{
"name":"name1",
"id":"2",
"website":"jingle"
}
]
Desired output:
[
{
"name":"name1",
"id":"1",
"website":"google"
},
{
"name":"name1",
"id":"1",
"website":"google"
}
]
how can I loop through arrays in a list and look for specific values for specific keys? Thanks for any help or ideas you can provide. I am a begginer with JSON and jq.
Enclose the select with a map, as you want to apply the filter to each array item individually while retaining the surrounding array structure.
jq '.items | map(select(.website == "google"))'
[
{
"name": "name1",
"id": "1",
"website": "google"
},
{
"name": "name1",
"id": "1",
"website": "google"
}
]
Demo

QJsonDocument::array() and QJsonDocument::object()

I am reading the QJsonDocument documentation and I use QJsonDocument with following line:
emit this->ueSignalNewDataArrivedPlaces(QJsonDocument::fromBinaryData(incomingData[0].toByteArray()));
and I do not understand, after this line, should I use QJsonDocument::array() or QJsonDocument::object(), i.e., in what situations does QJsonDocument create array and in what situations does create object?
A JSON array is an ordered list, it is written as:
[ <item1>, <item2>, <item3> ]
while a JSON object is a named list, written as:
{
<name1>: <item1>,
<name2>: <item2>
}
In Qt, a QJsonArray is equivalent to a QVariantList (QList<QVariant>) and a QJsonObject is equivalent to QVariantMap (QMap<QString, QVariant>).
Which one you have to use depends on the file you are parsing.
For instance, taking Wikipedia example:
{
"firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
},
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "office",
"number": "646 555-4567"
},
{
"type": "mobile",
"number": "123 456-7890"
}
],
"children": [],
"spouse": null
}
You would use a QJsonArray to get the list of phoneNumbers, each element of phoneNumbers is a QJsonObject whith 2 named values: type and number.
If in your code you need to manipulate a JSON element but you do not know its type you can use QJsonValue, which is one of: QJsonObject, QJsonArray, bool, double or a QString

Document Db query filter for an attribute that contains an array

With the sample json shown below, am trying to retrieve all documents that contains atleast one category which is array object wrapped underneath Categories that has the text value 'drinks' with the following query but the returned result is empty. Can someone help me get this right?
SELECT items.id
,items.description
,items.Categories
FROM items
WHERE ARRAY_CONTAINS(items.Categories.Category.Text, "drink")
{
"id": "1dbaf1d0-6549-11a0-88a8-001256957023",
"Categories": {
"Category": [{
"Type": "GS1",
"Id": "10000266",
"Text": "Stimulants/Energy Drinks Ready to Drink"
}, {
"Type": "GS2",
"Id": "10000266",
"Text": "Healthy Drink"
}]
}
},
Note: The json is a bit wierd to have the array wrapped by an object itself - this json was converted from a XML hence the result. So please assume I do not have any control over how this object is saved as json
You need to flatten the document in your query to get the result you want by joining the array back to the main document. The query you want would look like this:
SELECT items.id, items.Categories
FROM items
JOIN Category IN items.Categories.Category
WHERE CONTAINS(LOWER(Category.Text), "drink")
However, because there is no concept of a DISTINCT query, this will produce duplicates equal to the number of Category items that contain the word "drink". So this query would produce your example document twice like this:
[
{
"id": "1dbaf1d0-6549-11a0-88a8-001256957023",
"Categories": {
"Category": [
{
"Type": "GS1",
"Id": "10000266",
"Text": "Stimulants/Energy Drinks Ready to Drink"
},
{
"Type": "GS2",
"Id": "10000266",
"Text": "Healthy Drink"
}
]
}
},
{
"id": "1dbaf1d0-6549-11a0-88a8-001256957023",
"Categories": {
"Category": [
{
"Type": "GS1",
"Id": "10000266",
"Text": "Stimulants/Energy Drinks Ready to Drink"
},
{
"Type": "GS2",
"Id": "10000266",
"Text": "Healthy Drink"
}
]
}
}
]
This could be problematic and expensive if the Categories array holds a lot of Category items that have "drink" in them.
You can cut that down if you are only interested in a single Category by changing the query to:
SELECT items.id, Category
FROM items
JOIN Category IN items.Categories.Category
WHERE CONTAINS(LOWER(Category.Text), "drink")
Which would produce a more concise result with only the id field repeated with each matching Category item showing up once:
[{
"id": "1dbaf1d0-6549-11a0-88a8-001256957023",
"Category": {
"Type": "GS1",
"Id": "10000266",
"Text": "Stimulants/Energy Drinks Ready to Drink"
}
},
{
"id": "1dbaf1d0-6549-11a0-88a8-001256957023",
"Category": {
"Type": "GS2",
"Id": "10000266",
"Text": "Healthy Drink"
}
}]
Otherwise, you will have to filter the results when you get them back from the query to remove duplicate documents.
If it were me and I was building a production system with this requirement, I'd use Azure Search. Here is some info on hooking it up to DocumentDB.
If you don't want to do that and we must live with the constraint that you can't change the shape of the documents, the only way I can think to do this is to use a User Defined Function (UDF) like this:
function GetItemsWithMatchingCategories(categories, matchingString) {
if (Array.isArray(categories) && categories !== null) {
var lowerMatchingString = matchingString.toLowerCase();
for (var index = 0; index < categories.length; index++) {
var category = categories[index];
var categoryName = category.Text.toLowerCase();
if (categoryName.indexOf(lowerMatchingString) >= 0) {
return true;
}
}
}
}
Note, the code above was modified by the asker after actually trying it out so it's somewhat tested.
You would use it with a query like this:
SELECT * FROM items WHERE udf.GetItemsWithMatchingCategories(items.Categories, "drink")
Also, note that this will result in a full table scan (unless you can combine it with other criteria that can use an index) which may or may not meet your performance/RU limit constraints.

JSONAPI - Difference between self and related in a links resource

Why is the self and related references different in the below JSONAPI resource? Aren't they pointing to the same resource? What is the difference between going to /articles/1/relationships/tags and /articles/1/tags?
{
"links": {
"self": "/articles/1/relationships/tags",
"related": "/articles/1/tags"
},
"data": [
{ "type": "tags", "id": "2" },
{ "type": "tags", "id": "3" }
]
}
You can read about that here: https://github.com/json-api/json-api/issues/508.
Basically, with /articles/1/relationships/tags response will be object which represents relationship between articles and tags. The response could be something like this (what you put in your question):
{
"links": {
"self": "/articles/1/relationships/tags",
"related": "/articles/1/tags"
},
"data": [
{ "type": "tags", "id": "2" },
{ "type": "tags", "id": "3" }
]
}
This response gives only the necessary data (in primary data attribute - data) to manipulate the relationship and not resources connected with relationship. That being said, you'll call /articles/1/relationships/tags if you want to create new relationship, add a new tag (basically updating relationship) to article, read which tags belong to article (you only need identity to search them on server) or delete article tags.
On the other hand, calling /articles/1/tags will respond with tags as primary data with all the other properties that they have (articles, relationships, links, and other top-level attributes such include, emphasized text, links and/or jsonapi).
They are different. Here is an example from my project.
Try Get http://localhost:3000/phone-numbers/1/relationships/contact you will get response like this:
{
"links": {
"self": "http://localhost:3000/phone-numbers/1/relationships/contact",
"related": "http://localhost:3000/phone-numbers/1/contact"
},
"data": {
"type": "contacts",
"id": "1"
}
}
You didn't get the attributes and relationships which is probably you want to retrieve.
Then
Try Get http://localhost:3000/phone-numbers/1/contact you will get response like this:
{
"data": {
"id": "1",
"type": "contacts",
"links": {
"self": "http://localhost:3000/contacts/1"
},
"attributes": {
"name-first": "John",
"name-last": "Doe",
"email": "john.doe#boring.test",
"twitter": null
},
"relationships": {
"phone-numbers": {
"links": {
"self": "http://localhost:3000/contacts/1/relationships/phone-numbers",
"related": "http://localhost:3000/contacts/1/phone-numbers"
}
}
}
}
}
You can see you retrieved all the information you want, including the attributes and relationships.
But you should know that relationships can be used for some purpose. Please read http://jsonapi.org/format/#crud-updating-to-one-relationships as a sample.

JSONPath first occurrence of "gatherer"

I have a json file like this:
{
"cards": [
{
"artist": "Steve Argyle",
"images": {
"mtgimage": "http://mtgimage.com/set/pMPR/ponder.jpg"
},
},
{
"artist": "Mark Tedin",
"images": {
"gatherer": "http://gatherer.wizards.com/Handlers/Image.ashx?type=card&multiverseid=139512",
"mtgimage": "http://mtgimage.com/set/LRW/ponder.jpg"
},
},
{
"artist": "Dan Scott",
"images": {
"gatherer": "http://gatherer.wizards.com/Handlers/Image.ashx?type=card&multiverseid=190159",
"mtgimage": "http://mtgimage.com/set/M10/ponder.jpg"
},
}
]
}
I would like to get the first "gatherer" link from it using JSONPath.
I tried "$..gatherer[0]" but that doesn't work. However "$..gatherer" does give both instances:
[
"http:\/\/gatherer.wizards.com\/Handlers\/Image.ashx?type=card&multiverseid=139512",
"http:\/\/gatherer.wizards.com\/Handlers\/Image.ashx?type=card&multiverseid=190159"
]
How do I get only the first one? (without getting the last one in code, so only using a jsonpath string.)
(Tested with http://jsonpath.curiousconcept.com/ and in my program.)
You may need an interim step to save the array
var gatherers = $..gatherer; //however this gets it, I'm not sure
var first = gatherers[0];
that may work.

Resources