query on array value in moralis where the array contains objects - moralis

I was reading on how to query in moralis because I have to use something like that however, the docs does not seem to explain what I intend to do and I have been there for close to 2hrs and I seem to catch nothing to use
so my query is suppose to be close to the domo https://v1docs.moralis.io/moralis-dapp/database/queries#queries-on-array-values except in their case the array contained only number in my case it is an array of many objects like this
{
"name":"some name",
"attributes": [
{
"trait_type": "Background",
"value": "Aquamarine",
"rarityScore": 7.912183544303797
},
{
"trait_type": "Fur",
"value": "Gray",
"rarityScore": 20.163306451612904
},
{
"trait_type": "Clothes",
"value": "Pimp Coat",
"rarityScore": 125.0125
},
{
"trait_type": "Mouth",
"value": "Bored Unshaven Dagger",
"rarityScore": 357.1785714285714
},
{
"trait_type": "Eyes",
"value": "Closed",
"rarityScore": 14.105782792665725
},
{
"trait_type": "Hat",
"value": "Bayc Hat Black",
"rarityScore": 43.864035087719294
},
{
"trait_type": "TraitCount",
"value": 6,
"rarityScore": 15.04192517390487
},
{
"trait_type": "Earring",
"value": null,
"rarityScore": 1.4236298932384341
}
],
}
so what am doing is, my query should filter the objects on attributes where i supply trait_type and value
I tried
query.equalTo("attributes", filterQuery);
but it does not work coz in my case filterQuery is an array with different objects each with unique trait_type and value
and example of filterQuery am using is
[
{
"trait_type": "Fur",
"value": "Blue"
}
]
will appreciate very much if someone can help me solve this or even an idea on how it is done

Hi if attributes is a column of array with an array of objects value:
[
{
"trait_type": "Background",
"value": "Aquamarine",
"rarityScore": 7.912183544303797
},
{
"trait_type": "Fur",
"value": "Gray",
"rarityScore": 20.163306451612904
},
{
"trait_type": "Clothes",
"value": "Pimp Coat",
"rarityScore": 125.0125
},
{
"trait_type": "Mouth",
"value": "Bored Unshaven Dagger",
"rarityScore": 357.1785714285714
},
{
"trait_type": "Eyes",
"value": "Closed",
"rarityScore": 14.105782792665725
},
{
"trait_type": "Hat",
"value": "Bayc Hat Black",
"rarityScore": 43.864035087719294
},
{
"trait_type": "TraitCount",
"value": 6,
"rarityScore": 15.04192517390487
},
{
"trait_type": "Earring",
"value": null,
"rarityScore": 1.4236298932384341
}
]
Based on your filterQuery example, you could do something like:
query.equalTo("attributes.trait_type", "Fur");
query.equalTo("attributes.value", "Blue");
Which will only return an Object that has that particular trait_type and value combination in its attributes.
If the value of attributes is like the example you posted that has "name":"some name", you would use (again for a column name of attributes):
query.equalTo("attributes.attributes.trait_type", "Fur");
query.equalTo("attributes.attributes.value", "Blue");
If you have more questions or issues with this, please post on either:
Moralis Discord: https://moralis.io/joindiscord
Moralis forum: https://forum.moralis.io

Related

Metadata converter?

Im currently working on a generated collection and got a huge metadata file. Its build up like this:
[
{
"name": "amongnfts #1",
"description": "This is one member of the AmongNFT collection. It's unique and only exists once, but may be a bit cooler than the others.",
"image": "/1.png",
"dna": "055b93fd187e52f85e3ea8b0ad660fa51aaea9e0",
"edition": 1,
"date": 1645368881553,
"attributes": [
{
"trait_type": "background",
"value": "blue"
},
{
"trait_type": "body",
"value": "pink"
},
{
"trait_type": "clothing",
"value": "teamred"
},
{
"trait_type": "hat",
"value": "pilot"
},
{
"trait_type": "visor",
"value": "krieghaus"
},
{
"trait_type": "pet",
"value": "minimateorange"
}
],
"compiler": "HashLips Art Engine"
},
{
"name": "amongnfts #2",
"description": "This is one member of the AmongNFT collection. It's unique and only exists once, but may be a bit cooler than the others.",
"image": "/2.png",
"dna": "a05e9acf4665b9e9e5adbcb9dfc33df255e2e58f",
"edition": 2,
"date": 1645368883622,
"attributes": [
{
"trait_type": "background",
"value": "orange"
},
{
"trait_type": "body",
"value": "white"
},
{
"trait_type": "clothing",
"value": "elf"
},
{
"trait_type": "hat",
"value": "wethair"
},
{
"trait_type": "visor",
"value": "none"
},
{
"trait_type": "pet",
"value": "minimatetan"
}
],
"compiler": "HashLips Art Engine"
},
]
(Goes down for 10.000 editions so thats why I dont do it manually.)
But it should look like this:
{
"nft": [
{
"file_path": "/1.png",
"nft_name": "amongnfts #1",
"external_link": "",
"description": "This is one member of the AmongNFT collection. It's unique and only exists once, but may be a bit cooler than the others.",
"collection": "AmongNFTs",
"properties": [
{
"type": "background",
"name": "blue"
},
{
"type": "body",
"name": "pink"
},
{
"type": "clothing",
"name": "teamred"
},
{
"type": "hat",
"name": "pilot"
},
{
"type": "visor",
"name": "krieghaus"
},
{
"type": "pet",
"name": "minimateorange"
},
],
"levels": "",
"stats": "",
"unlockable_content": "",
"explicit_and_sensitive_content": "",
"supply": "1",
"blockchain": "Polygon",
"sale_type": "",
"price": 0.001,
"method": "",
"duration": "",
"specific_buyer": "",
"quantity": ""
}
]
}
Is there an easy way of converting them automatically and if so, how would the code look like?
The metadata.json file is here.

JSON-Path for objects that contain JSON array with specific entry

I have a JSON-Object containing a list of events related to soccer matches, as posted below.
I am using Newtonsoft's Json.NET framework. In said JSON-Object, what would be the JSON-Path (that works in Json.NET's JSONPath implementation) to find only events where there is a qualifier that has type.displayName 'Yellow'` (i.e., a JSON Path that returns only the second event, since this one has the desired qualifier)?
This is in .NET, so if there is a way to use LINQ for this, I'm open to that.
{
"events": [
{
"eventId": 490,
"minute": 54,
"second": 23,
"period": {
"value": 2,
"displayName": "SecondHalf"
},
"type": {
"value": 19,
"displayName": "SubstitutionOn"
},
"outcomeType": {
"value": 1,
"displayName": "Successful"
},
"qualifiers": [
{
"type": {
"value": 55,
"displayName": "RelatedEventId"
},
"value": "489"
},
{
"type": {
"value": 59,
"displayName": "JerseyNumber"
},
"value": "11"
}
],
"satisfiedEventsTypes": [
212
],
"isTouch": false
},
{
"eventId": 669,
"minute": 75,
"second": 5,
"period": {
"value": 2,
"displayName": "SecondHalf"
},
"type": {
"value": 17,
"displayName": "Card"
},
"outcomeType": {
"value": 1,
"displayName": "Successful"
},
"qualifiers": [
{
"type": {
"value": 13,
"displayName": "Foul"
},
"value": "243"
},
{
"type": {
"value": 31,
"displayName": "Yellow"
}
}
],
"isTouch": false
}
]
}
It turns out the correct JSON-Path in Json.Net for this would be
$.events[?(#.qualifiers..type.displayName == 'Yellow')]
Specifically, the deepscan operator .. is necessary, since the JSON in question may contain more than one qualifier per event.

API JSON to CSV or Google Sheets

I want to sort the values in csv (or google sheet)with an API. But my knowledge of JSON=0. I'm not sure how to retrieve the right data.
Also i dont know how to sort JSON (xD)
import json
import requests
import csv
api_key = "https://api.blablablbla"
r = requests.get('https://api.blablablbla')
packages_json = r.json()
The sorting should by header name: projectnumber, project name etc. and the rows should be the "value" corrosponding the header. I feel like the imput of the data is not perfect. The input is as follows(parsed):
{
"results": [
{
"submittedAt": 1614950496814,
"values": [
{
"name": "projectnumber?",
"value": "123",
"objectTypeId": "0-1"
},
{
"name": "project name?_",
"value": "Project X",
"objectTypeId": "0-1"
},
{
"name": "email",
"value": "abc#123.com",
"objectTypeId": "0-1"
},
{
"name": "firstname",
"value": "James",
"objectTypeId": "0-1"
},
{
"name": "lastname",
"value": "Bond",
"objectTypeId": "0-1"
},
{
"name": "Are you available?_",
"value": "Maybe later",
"objectTypeId": "0-1"
},
{
"name": "Send reminder?_",
"value": "Yes",
"objectTypeId": "0-1"
},
{
"name": "form filled in by",
"value": "Jimmy",
"objectTypeId": "0-1"
}
],
"pageUrl": "https://share.hsforms.com/.........."
},
The second 1:
{
"submittedAt": 1614943909980,
"values": [
{
"name": "projectnumber?",
"value": "1234",
"objectTypeId": "0-1"
},
{
"name": "project name?",
"value": "XYZ",
"objectTypeId": "0-1"
},
{
"name": "email",
"value": "bca#bobs.nl",
"objectTypeId": "0-1"
},
{
"name": "firstname",
"value": "Jan",
"objectTypeId": "0-1"
},
{
"name": "lastname",
"value": "de Vries",
"objectTypeId": "0-1"
},
{
"name": "Are you available?",
"value": "maybe later",
"objectTypeId": "0-1"
},
{
"name": "Send reminder",
"value": "yes",
"objectTypeId": "0-1"
}
],
"pageUrl": "https://share.hsforms.com/........."
},
Help!
I think in this solution but probably if I knew the context the aswer would be more acurate. In first place you should receive the parameters of where you want to insert the values. For example, you use the google sheet API the json will be something like this:
{
"connection":{
"workbookID": "ABC",
"sheetName": "ABC"
}
"values": [
{
"name": "column1",
"values": ["rowA", "rowB", "rowC"]
},
{
"name": "column2",
"values": ["rowA", "rowB", "rowC"]
}]
}
In the same way, you can change the connections to get the CSV parameters and parce the values on it.
Then you can read the values as arrays and insert them without complication.

Jsonpath querying only one item

I have a problem to find out only a determined value in a json using jsonpath.
I have this json:
{"tvs": {
{ "tv": [
{
"serial": "HD1300",
"data": [
{
"title": "manufacturer",
"value": "lg"
},
{
"title": "color",
"value": "silver"
},
{
"title": "inches",
"value": 32
},
{
"title": "connection",
"value": 220
},
{
"title": "connection",
"value": 400
}
]
}.. more tvs
And I want to know if the value connection:400 is present for serial hd1300
I already tried with:
$.tvs.[?(#.serial=='hd1340')].data.[?(#.title== 'connection'),(#.value==400)]
But my problem is that I retrieve also the "connection" with 200. How can I filter to get only this value?
I think you might have an error in your JSON (extra brace between tvs and tv). I was able to get this to work on http://jsonpath.com.
{"tvs":
{ "tv": [
{
"serial": "HD1300",
"data": [
{
"title": "manufacturer",
"value": "lg"
},
{
"title": "color",
"value": "silver"
},
{
"title": "inches",
"value": 32
},
{
"title": "connection",
"value": 220
},
{
"title": "connection",
"value": 400
}
]}
]}
}
$.tvs.tv.[?(#.serial=='HD1300')].data.[?(#.title=='connection' && #.value=='400')]

how to remove key when the value is empty in json

I want to remove key when value is null in JSON for example (using JAVA)
"attributes": {
"csapDescriptionOfNart": {
"values": []
},
"cpimCustomerWishDateOfDelivery": {
"alias": "Customer Wish Date Of Delivery",
"values": [{
"value": "12/21/2016",
"source": "SAP",
"locale": "en-us"
}]
}
}
In this csapDescriptionOfNart has to removed
"attributes": {
"cpimCustomerWishDateOfDelivery": {
"alias": "Customer Wish Date Of Delivery",
"values": [{
"value": "12/21/2016",
"source": "SAP",
"locale": "en-us"
}]
}
}