Getting json keys from an array from an object using ArduinoJson - esp32

I'm having trouble getting the keys (and values) from "prefs" in the following json.
{
"cmd": "set",
"prefs": [
{
"coins": 4
},
{
"enable": true
}
]
}
Code to process json:
DynamicJsonDocument doc(1024);
deserializeJson(doc,"{\"cmd\":\"set\",\"prefs\":[{\"coins\":4},{\"enable\":true}]}");
JsonObject root=doc.as<JsonObject>();
for (JsonPair kv : root) {
Serial.println(kv.key().c_str());
Serial.println(kv.value().as<char*>());
}
JsonObject prefs=doc["prefs"];
for (JsonPair kv : prefs) {
Serial.println("here\n");
Serial.println(kv.key().c_str());
// Serial.println(kv.value().as<const char*>());
}
I would expect to see the following output:
cmd
set
prefs
coins
enable
But I only get what seems to be an empty prefs object:
cmd
set
prefs
The example shown in the official docs almost gets me there, and is what I have in my code. This example from github is similar, but I can't seem to adapt it to my case.

Since prefs is an array, convert it to JsonArray
JsonArray prefs = doc["prefs"].as<JsonArray>();
for (JsonObject a : prefs) {
for (JsonPair kv : a) {
Serial.println(kv.key().c_str());
if (kv.value().is<int>()) {
Serial.println(kv.value().as<int>());
}
else {
Serial.println(kv.value().as<bool>());
}
}
}

Related

GitHub GraphQL filter multi variable language filter

I'm trying to use GitHub's GraphQL API to find a list of repos matching a query but limited to a specific language. However, I can't find anything in the docs relating to the multi variable language filter the typical online search supports or how something like this is typically done with GraphQL.
{
search(query: "language:java", type: REPOSITORY, first: 10) {
repositoryCount
edges {
node {
... on Repository {
nameWithOwner
forkCount
hasIssuesEnabled
hasProjectsEnabled
homepageUrl
id
}
}
}
}
}
I want to pass two params on language and show the result but this query just use string to search. I need to send a request as multi items like this
language:['go','java','javaScript']
As a workaround, you can use aliases to build dynamic query with many search query targetting a specific language and fragments to avoid repetition of the SearchResultItemConnection in the query :
{
go: search(query: "language:go", type: REPOSITORY, first: 10) {
...SearchResult
}
java: search(query: "language:java", type: REPOSITORY, first: 10) {
...SearchResult
}
javascript: search(query: "language:javascript", type: REPOSITORY, first: 10) {
...SearchResult
}
}
fragment SearchResult on SearchResultItemConnection {
repositoryCount
edges {
node {
... on Repository {
nameWithOwner
forkCount
hasIssuesEnabled
hasProjectsEnabled
homepageUrl
id
}
}
}
}
Try it in the explorer
Note that it would only work for OR query (java or javascript or go for the list of languages) but not AND
The request can be built programmatically such as in this python script :
import requests
token = "YOUR_TOKEN"
languages = ["go","java","javaScript"]
query = """
{
%s
}
fragment SearchResult on SearchResultItemConnection {
repositoryCount
edges {
node {
... on Repository {
nameWithOwner
forkCount
hasIssuesEnabled
hasProjectsEnabled
homepageUrl
id
}
}
}
}
"""
searchFragments = "".join([
"""
%s: search(query: "language:%s", type: REPOSITORY, first: 10) {
...SearchResult
}
""" % (t,t) for t in languages
])
r = requests.post("https://api.github.com/graphql",
headers = {
"Authorization": f"Bearer {token}"
},
json = {
"query": query % searchFragments
}
)
print(r.json()["data"])

Is there a way to filter out null values for literals and references

If we have an Author with no beacons to Articles and thus WroteArticles was null and we wanted to only return Authors who had non-empty/non-null WroteArticles, how could that be done?
As an example we can use the Weaviate demo site
I've tried filter operations using where and various operators, but I must be missing something obvious. Example of a query I've tried on my own data set below, where I did have a Thing with no beacons.
{
Get {
Things {
Author (where:{
operator:Equal,
path:["WroteArticles"]
valueString:" "
}){
name
WroteArticles {
... on Article {
InPublication {
... on Publication {
name
}
}
}
}
}
}
}
}
You can now do this as follows (also in the documentation):
{
Get {
Things {
Author(
where:{
valueInt: 2
operator:GreaterThanEqual
path: ["WroteArticles"]
}
) {
name
WroteArticles {
... on Article {
title
}
}
}
}
}
}

Contentful graphql api not fetching localized `linkedfrom` items

Basically I want to get localized values for the entries linking to my unique entry.
movie(id: $movieId) {
linkedFrom {
spanishMovieLocations: movieLocationCollection(locale: "es-ES") {
items {//fields with localized values}
}
}
}
But instead I get no results in my collection array. Querying directly for movieLocationCollection(locale: "es-ES") gets me localized values and has some matching movie.sys.id that is used in $movieId.
This works fine, but then I dont get the localized fields I need.
movie(id: $movieId) {
linkedFrom {
movieLocationCollection {
items {//fields with non-localized values}
}
}
}
from the contentful site:
We ... made the querying of localized reference content easier by introducing a new allowedLocales argument.
https://www.contentful.com/blog/2020/12/03/three-new-graphql-features-2020/
movie(id: $movieId) {
linkedFrom(allowedLocales: ["es-ES"]) {
movieLocationCollection {
items {//fields with non-localized values}
}
}
}
In my project I had to include both locales
query ($parner: String) {
partnerCollection(limit: 1, locale: "fr-CA", where: {title: $parner}) {
items {
title
sys {
id
}
linkedFrom(allowedLocales: ["en-US", "fr-CA"]) {
benefitCollection(limit: 3) {
total
items {
title
}
}
}
}
}
}

GraphQL fallback query if no results

I have the following query:
{
entity(id: "theId") {
source1: media(source: 1){
images{
src, alt
}
}
source2: media(source: 2){
images{
src, alt
}
}
}
}
That give me a result like:
{
"entity": [
{
"source1": {
"images": [{"src": "", "alt": ""}]
},
"source2": {
"images": [{"src": "", "alt": ""}]
}
}
]
}
Is there a way to have a single result of source1 and source2, executing source1 and if it has no result it use source2 as fallback?
You are querying two fields (source1, source2) so something has to come back for both of them (null being a possible option). If you want to check them in a sequence you should probably break the query in two and run them one at the time from the client.
Could you perhaps change so you only query a single source field and have the resolver (on the server) return what makes sense based on what is available, so to speak? Like this:
{
entity(id: "theId") {
source: media(sourcesList: [1, 2]){
images{
src, alt
}
}
}
}
where sourceList is the sources to try, in order. So the resolver (server) can then check if source 1 is available and if not return source 2.
You could also add a field to let the client know which source was actually returned from the proposed list (sourceNumberReturned below would return 1 if source 1 was returned, otherwise 2).
{
entity(id: "theId") {
source: media(sourcesList: [1, 2]){
images{
src, alt
}
sourceNumberReturned
}
}
}

how to query prismic slices and returning data from each slice

I'm trying to use Gatsby's /___graphq debugger and the README file for gatsby-source-prismic says you can return slices. So below I'm returning the slice with a name PrismicProductBodySteps.
{
allPrismicHomePage {
edges {
node {
data {
seo_title
body {
__typename
... on PrismicProductBodySteps {
}
}
}
}
}
}
}
}
Can someone explain to me what ... on PrismicProductBodySteps means ?
In a gatsby component I've seen this as an example.
body {
... on PrismicProductsBodySteps {
...ProductStepsFragment
}
Can anyone explain to me what the ...ProductStepsFragment means ?
PrismicProductBodySteps would be a custom node type name representing a dynamic series of content blocks. That custom node type name is coming from a Prismic data model; yours will likely be different.
According to the gatsby-source-prismic documentation, using custom node type names requires you to figure out what they are first:
The easiest way to get the type of nodes is to use the /___graphql
debugger and run the below query (adjust the document type and field
name).
{
allPrismicPage {
edges {
node {
id
data {
body {
__typename
}
}
}
}
}
}
Once you have your custom node type name, you can use a GraphQL fragment to pull data specific to each fragment. Again, this would depend on how you have the fragments defined in your data model, but it would look something like this:
{
allPrismicHomePage {
edges {
node {
data {
seo_title
body {
__typename
... on PrismicYourContentBlockOne {
text {
html
}
}
... on PrismicYourContentBlockTwo {
text {
html
}
}
... on PrismicYourContentBlockThree {
text {
html
}
}
}
}
}
}
}
}

Resources