Select nested json into columns/rows - postgresql-13

I'm using PostgreSQL 13.6. I have a buttons json column on a table. The buttons columns contains an array of button settings, similar to, but not always like this. This is one of the more complex ones. There are multiple buttons in each json blob, so I need to loop over them.
[{
"label": "Submit",
"action": ["submit"]
}, {
"label": "New",
"action": ["new"]
}, {
"label": "Profile this",
"action": ["copyToOwn"],
"activeOn": {
"record": ["profile"],
"displayMode": ["Read Only"],
"existingOnly": true
}
}]
Here's a couple more examples:
[{"label":"Submit","action":"submit"}] --Single button
[{"label":"Reset","action":"reset"},{"label":"Search","action":"search"}] --Two buttons
[{"label":"Reset","action":["reset"],"activeOn":"profile"},{"label":"Search","action":["redirect"],"redirect":["url_alias"],"redirect_template":"/render/id"}] -- Two buttons with different settings
I want to make a new row in a buttons table for each button of each row. So label, action, active_on, record, display_mode, existing_only, redirect, and redirect_template would be columns.
The tables are like this
create table structure.button (
id integer not null
action text not null,
description text not null,...
create table structure.button_config (
button_id integer not null,
redirect_template text,
redirect text,
active_on text,
existing_only boolean,
record integer,...
So a row might look like
button table
id label action
1 Reset reset
button_config
button_id redirect_template redirect active_on ...
1 /render/id url_alias profile
This could be a function or a query. I have basically full access to the DB. I just cannot seem to figure out looping over json inside postgres.

Related

Logic Apps - I can create a row in dataverse with null in lookup field, but i can't update a row with null

When I use the dataverse connector 'create a row (preview)', I can create a row in the table 'Account' with the field 'primarycontactid' null. When I try do this with 'update a row (preview)' however, I get an error:
{
"error": {
"code": "0x80060888",
"message": "The supplied reference link -- -- is invalid. Expecting a reference link of the form /entityset(key)."
}
}
this is the body:
"body": {
"name": "Test2",
"primarycontactid#odata.bind": ""
}
When I adjust the code itself to "primarycontactid#odata.bind": null, it works.
but i need to have something like this (so this works on create, not update):
"primarycontactid#odata.bind": "#{if(empty(body('Get_Contact')?['value']),null,concat('contacts(',body('Get_Contact')?['value']?[0]?['contactid'] ,')'))}"
I tried filling it with null, with empty quotes, with contacts(00000000-0000-0000-0000-000000000000) but nothing works.
After reproducing from my end, I have received the same error but after following the below process, I could able to update the field with null value. Below is the flow of my Logic app.
In my codeview
"body": {
"crc76_primarycontactid": "#{null}"
}
Results
Before updating
After updating

DynamoDB Update Expression For list in a Map

I have following dynamoDB Schema:
{
"Id": {"N": "789"},
"ProductCategory": {"S": "Home Improvement"},
"productReviews": {
"M": {
"FiveStar": {
"L": [
{ "S": "Best product ever!" }
]
},
"FourStar": {
"L": [
{ "S": "Good product" },
{ "S": "Another Review" }
]
}
}
}
}
So basically I have a map productReviews which has key as - "FourStar", "FiveStar", "TwoStar" etc. and value as List of reviews.
I want add new reviews to this table i.e. if a fiveStar reviews comes I will add/append it in the list of 'FiveStar' of productReviews. If a key does not exist in the map, I would like to just append the key value.
Is this possible in DynamoDB or I have to merge the list on my own and then update at each write.
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html#Expressions.UpdateExpressions.SET
If you want this model, you'll need to merge and update on your own.
It might be simpler to make each star value its own item (give them all the same PK with an SK indicative of the star value) so you can update them directly as a single item with a string list to append to.
Depending on your access patterns, you might even want each review as its own item (still under the same PK). For example, if the item gets a thousand reviews coming in over time, you don't want to repeatedly update an ever growing item if you can just blindly add a small item each time.
On retrieval it'll still be efficient with a Query for the PK.

How do I update a field in DynamoDB?

I would like to update a field in my DynamoDB table called 'AppTable' using the AWS-Amplify methods that were generated when creating the corresponding CRUD cloud-api (e.g. PUT, GET, POST, DELETE).
The API methods reside in awsmobilejs/backend/cloud-api/AppTable/app.js
I can simply use API.put to add to this table. e.g:
apiResponse = API.put('AppTableCRUD', path, body)
body: {
"uploaderBool": true,
"userId": 'user1',
"itemId": '10005',
"Username": "first_app_user",
"Email": "user#myapp.com",
"NumberList":
[
"7.9",
"5.7",
"3.4",
"4.9"
],
"AverageNumber":[
"5.5"
],
}
}
What i want to do, is update the NumberList field with a function in my react-native app, count how many numbers in the list on DynamoDB, then calculate/write an updated number called AverageNumber.
If i use a current API.put method i have to put in this ENTIRE body everytime (this list could be hundreds of thousand of entries so Get, then Put with a single update is absurd).
How do i just append a single number to the NumberList and update the AverageNumber fields?

JSON Schema: verifying object's values, without keys

Not to confuse anybody, I'll start with validating arrays...
Regarding arrays, JSON Schema can check whether elements of an (((...)sub)sub)array conform to a structure:
"type": "array",
"items": {
...
}
When validating objects, I know I can pass certain keys with their corresponding value types, such as:
"type": "object",
"properties": {
// key-value pairs, might also define subschemas
}
But what if I've got an object which I want to use to validate values only (without keys)?
My real-case example is that I'm configuring buttons: there might be edit, delete, add buttons and so on. They all have specific, rigid structure, which I do have JSON schema for. But I don't want to limit myself to ['edit', 'delete', 'add'] only, there might be publish or print in the future. But I know they all will conform to my subschema.
Each button is:
BUTTON = {
"routing": "...",
"params": { ... },
"className": "...",
"i18nLabel": "..."
}
And I've got an object (not an array) of buttons:
{
"edit": BUTTON,
"delete": BUTTON,
...
}
How can I write such JSON schema? Is there any way of combining object with items (I know there are object-properties and array-items relations).
You can use additionalProperties for this. If you set additionalProperties to a schema instead of a boolean, then any properties that aren't explicitly declared using the properties or patternProperties keywords must match the given schema.
{
"type": "object",
"additionalProperties": {
... BUTTON SCHEMA ...
}
}
http://json-schema.org/latest/json-schema-validation.html#anchor64

Simple request using googleapis to freebase

I would like to query freebase, to get some basic information about a celebrity.
For example, I would like to get the place of birth, and the gender of Madonna.
I achieved to get Madonna's friends by using:
https://www.googleapis.com/freebase/v1/mqlread?&query={"id":"/en/madonna","name":null,"type":"/base/popstra/celebrity","/base/popstra/celebrity/friendship":[{"participant":[]}]}
But, I'm understanding this request pretty badly. How can I change it to get the information I talked above ?
I was thinking about :
https://www.googleapis.com/freebase/v1/mqlread?&query={"id":"/en/madonna","name":null,"type":"/base/popstra/celebrity","/base/popstra/celebrity/gender":[{"gender":}], "/base/popstra/celebrity/place_of_birth":[{"place of birth":}]}
but it's not working ofc.
Here is how you build MQL queries for Freebase topics:
Find a sample topic that meets you query (ex. Madonna)
Go the the Query Editor and build a simple query for the ID of that topic:
[{
"id": "/en/madonna",
"name": null
}]​
Go to the sample topic page, click on "Edit this topic" and find the facts that you want to query for (ex. Date of birth)
You can see that Date of birth is part of the Person (/people/person) type.
Click on little wrench icon to the right of Person to go to the schema page for that type.
From there, you can see that the property that stores the date of birth is called /people/person/date_of_birth.
Add the properties that you want to your query like so:
[{
"id" "/en/madonna",
"name": null,
"/people/person/date_of_birth": null,
"/people/person/gender": null
}]​
Queries can also specify a default type (ex. /people/person) which makes the property paths shorter:
[{
"id" "/en/madonna",
"name": null,
"type": "/people/person",
"date_of_birth": null,
"gender": null
}]​
But each level of query can only have one default type so if you want to mix in properties from other types you'll need to use the full property IDs:
[{
"id": "/en/madonna",
"name": null,
"type": "/people/person",
"date_of_birth": null,
"gender": null,
"/base/popstra/celebrity/friendship": [{
"participant": []
}]
}]​
When you've got your query working in the query editor, just click the "Link" menu at the top right to get the MQLRead Link that will make that query to our API.

Resources