Why is the json code wrong? - json

Why is the json code wrong? I know I can have multi key in XML, but it seem that json doesn't allow.
{
"BackupSettings": {
"Setting":
{
"id": "34345"
},
"Setting": {
"id": "16454"
}
}
}

Indeed, keys within an object are required to be unique in JSON. The canonical way of expressing your data in JSON would be to use an array. It could look something like the following:
{
"BackupSettings": {
"Settings": [
{
"id": "34345"
},
{
"id": "16454"
}
]
}
}
Or even:
{
"BackupSettings": [
{
"id": "34345"
},
{
"id": "16454"
}
]
}

Related

Presto Json Parsing

I have a json field(attached sample) and i need to extract the values in ProvisioningSystem path but it works only if i hardcode the array location.How can i extract the value without hardcoding ?Thanks in Advance!
Code:
TRANSFORM(CAST(JSON_EXTRACT(order_json, '$.Order.Accounts.Account') AS ARRAY), x -> JSON_EXTRACT_SCALAR(x,'$.ProvisioningSystems.ProvisioningSystem[1].SystemName'))
Json:
{
"Order":
{
"Accounts": {
"Account": [
{
"ProvisioningSystems": {},
},
{
"ProvisioningSystems": {
"ProvisioningSystem": [
{
"SystemOrderRef": "12345",
"SystemName": "Testsystem",
"SystemOrderRefType": "Provision"
}
]
},
}
]
},
}
}
}

JsonPath query?

I have a JSON file like this:
{
"Resources": {
"myresource1": {
"Properties": {
"keyA": {
"Ref": "resource2"
},
"keyB": "something",
"keyC": {
"another object": {
"Ref": "resource3"
}
}
}
},
"resource2": {
"Properties": {
"keyA": 1,
"keyB": 2
}
},
"resource3": {}
}
}
I'd like a JSON Path query that finds all Resources that have Properties that have a Ref object in them.
So in the JSON above, myresource1 has two properties that satisfy this condition and the Refs are resource2 and resource3.
Is this possible?
I found this query works:
$..Properties.*..Ref
It gives me a list of paths:
[
"$['Resources']['myresource1']['Properties']['keyA']['Ref']",
"$['Resources']['myresource1']['Properties']['keyC']['another object']['Ref']"
]
Is there a better solution?

Create Firestore database documents from Postman

I'm trying to create multiple documents at once in a Firestore collection with configuration purposes. I can create documents one by one sending from Postman:
{
"fields": {
"category":{"stringValue": "Configs"},
"order":{"integerValue":"3"}
}
}
I was wondering if there is some way to create multiple documents sending something like this:
{
"batch": [
{
"fields": {
"categoria": {
"stringValue": "Configurações"
},
"ordem": {
"integerValue": "3"
}
}
},
{
"fields": {
"categoria": {
"stringValue": "Configurações"
},
"ordem": {
"integerValue": "3"
}
}
}
]
}
Can anyone say if is it possible? If so, how can I do that?
Something like the below should work.
Although it is an "update" action is creates new documents as well if they don't exist. The document ID must be provided, it cannot be auto generated using the commit endpoint.
I was able to get my use case to work after viewing this response detailing use of commit.
API Endpoint: https://firestore.googleapis.com/v1/projects/projectID/databases/(default)/documents:commit
{
"writes": [
{
"update": {
"name": "projects/projectID/databases/(default)/documents/test/ExistingDocumentID1",
"fields": {
"comment": {
"stringValue": "Hello World!"
}
}
}
},
{
"update": {
"name": "projects/projectID/databases/(default)/documents/test/NewManualDocumentID2",
"fields": {
"comment": {
"stringValue": "Happy Birthday!"
}
}
}
}
]
}

How to iterate through data from a JSON file in VueJS/Gridsome using GraphQL?

I am getting my feet wet in VueJS/Gridsome and I am trying to make a simple site to start off. What I am looking to do is store all my data locally in a JSON file and pull it into my page/component using GraphQL. I am able to see the data load however I cannot iterate through every object; instead it shows me all the data.
I have tried changing the way I layout my JSON objects schemas, tried separating into multiple json files (Do not want to do this) and have played with different v-for parameters. On the Gridsome Docs and Youtube Tutorials everyone is pulling from markdown and generating pages from each markdown file or using some sort of API. I want to accomplish this in a single JSON file.
videos.json
[
{
"title": "vid 1",
"url": "url1"
},
{
"title": "vid 2",
"url": "url2"
}
]
GraphQL Query
{
allVideo {
edges {
node{
id
data {
title
url
}
}
}
}
}
GraphQL Results
{
"data": {
"allVideo": {
"edges": [
{
"node": {
"id": "6b3cddf43cce8f8a0fb122d194db6edc",
"data": [
{
"title": "vid 1",
"url": "https://youtube.com"
},
{
"title": "vid 2",
"url": "https://youtube.com"
}
]
}
}
]
}
}
}
Videos.vue
<template>
<Layout>
<h1>Videos</h1>
<div v-for="edge in $page.allVideo.edges" :key="edge.node.id">
{{edge.node.data}}
</div>
</Layout>
</template>
<page-query>
query Videos {
allVideo {
edges {
node {
id
data {
title
url
}
}
}
}
}
</page-query>
Page Result:
Videos
[ { "title": "vid 1", "url": "url1" }, { "title": "vid 2", "url": "url2" } ]
I expect there to be an ID for each video entry, however it seems that the data collection is in the wrong scope and I cannot get it one level up to be with ID to generate an ID with each set of video.
I am looking for something that will give me this query result:
{
"data": {
"allVideo": {
"edges": [
{
"node": {
"id": "6b3cddf43cce8f8a0fb122d194db6edc",
"data": [
{
"title": "vid 1",
"url": "url1"
}
]
}
},
{
"node": {
"id": "some other ID",
"data": [
{
"title": "vid 2",
"url": "url 2"
}
]
}
}
]
}
}
}
You can do this by querying a single video using the GraphQL query below.
If you check the docs on GraphQL playground on http://localhost:8081/___explore you should see a query for Video and also allVideo. Instead of using allVideo in your vue components use a single video and pass in the ID you should get the result you want.
{
Video(id: "6b3cddf43cce8f8a0fb122d194db6edc") {
edges {
node{
id
data {
title
url
}
}
}
}
}

How to check in elasticsearch if a JSON object has a key using the DSL?

If I have two documents within an index of the following format, I just want to weed out the ones which have an empty JSON instead of my expected key.
A
{
"search": {
"gold": [1,2,3,4]
}
B
{
"search":{}
}
I should just get A json and not B json.
I've tried the exists query to search for "gold" but it just checks for non null values and returns the list.
Note: The following doesn't do what I want.
GET test/_search
{
"query": {
"bool": {
"must": [
{
"exists": { "field": "search.gold" }}
]
}
}
}
This is a simple question but I'm unable to find a way to do it even after searching through their docs.
If someone can help me do this it would be really great.
The simplified mapping of the index is :
"test": {
"mappings": {
"carts": {
"dynamic": "true",
"_all": {
"enabled": false
},
"properties": {
"line_items": {
"properties": {
"line_items_dyn_arr": {
"type": "nested",
"properties": {
"dynamic_key": {
"type": "keyword"
}
}
}
}
}
}
}
}
}
Are you storing complete json in search field?
If this is not the case then please share the mapping of your index and sample data.
Update: Query for nested field:
{
"query": {
"nested": {
"path": "search",
"query": {
"bool": {
"must": [
{
"exists": {
"field": "search.gold"
}
}
]
}
}
}
}
}
For nested type fields we need to specify the path and query to be executed on nested fields since nested fields are indexed as child documents.
Elastic documentation: Nested Query
UPDATE based on the mapping added in question asked:
{
"query": {
"nested": {
"path": "line_items.line_items_dyn_arr",
"query": {
"exists": {
"field": "line_items.line_items_dyn_arr"
}
}
}
}
}
Notice that we used "path": "line_items.line_items_dyn_arr". The reason we require to provide full path is because nested field line_items_dyn_arr is itself under line_items object. Had line_items_dyn_arr be a property of mapping and not the property of object or nested field the previous query would work fine.
Nishant's answer is right but for some reason I could get it working only if the path and field are the whole paths.
The following works for me.
{
"nested": {
"path": "search.gold",
"query": {
"exists": {
"field": "search.gold"
}
}
}
}