I have a database of a couple million records that look like this:
{
"id": "aa1e24cd-2825-490f-8ccb-60bd4914dc20",
"circulationNotes": [
{
"note": "REFERENCE BOOK",
"noteType": "Check in"
},
{
"note": "REFERENCE BOOK",
"noteType": "Check out"
}
],
"holdingsRecordId": "fd79910f-4d11-41b4-9e53-198fff089917",
"permanentLocationId": "cee4d952-da5a-4d34-bb0a-a5d4d4581f39"
}
I need to generate and add a uuid to each of the circulationNotes lacking them (~ 10K records) so the desired output is
{
"id": "aa1e24cd-2825-490f-8ccb-60bd4914dc20",
"circulationNotes": [
{
"id": "3794824a-b814-4b94-b7c4-d8be53088a51",
"note": "REFERENCE BOOK",
"noteType": "Check in"
},
{
"id": "175989f9-16f4-4cfe-a1aa-d665ec1263e7",
"note": "REFERENCE BOOK",
"noteType": "Check out"
}
],
"holdingsRecordId": "fd79910f-4d11-41b4-9e53-198fff089917",
"permanentLocationId": "cee4d952-da5a-4d34-bb0a-a5d4d4581f39"
}
I can select the records I want to update with the query:
SELECT id
FROM mod_inventory_storage.item
WHERE EXISTS
( SELECT *
FROM jsonb_array_elements(jsonb->'circulationNotes')
WHERE value->'note' IS NOT NULL
AND value->'id' IS NULL )
(data is such that if one note has the problem, both do)
but how do I update the jsonb object with uuids in each entry within circulationNotes?
I replicated your case with the following
create table test (id serial, text jsonb);
insert into test (text) values ('{
"id": "aa1e24cd-2825-490f-8ccb-60bd4914dc20",
"circulationNotes": [
{
"note": "REFERENCE BOOK",
"noteType": "Check in"
},
{
"note": "REFERENCE BOOK",
"noteType": "Check out"
}
],
"holdingsRecordId": "fd79910f-4d11-41b4-9e53-198fff089917",
"permanentLocationId": "cee4d952-da5a-4d34-bb0a-a5d4d4581f39"
}');
insert into test (text) values ('{
"id": "aa1e24cd-2825-490f-8ccb-60bd4914dc20",
"circulationNotes": [
{
"note": "REFERENCE BOOK",
"noteType": "Check in"
},
{
"note": "REFERENCE BOOK",
"noteType": "Check out"
}
],
"holdingsRecordId": "fd79910f-4d11-41b4-9e53-198fff089917",
"permanentLocationId": "cee4d952-da5a-4d34-bb0a-a5d4d4581f39"
}');
insert into test (text) values ('{
"id": "aa1e24cd-2825-490f-8ccb-60bd4914dc20",
"circulationNotes": [
{
"note": "REFERENCE BOOK",
"noteType": "Check in"
},
{
"note": "REFERENCE BOOK",
"noteType": "Check out"
}
],
"holdingsRecordId": "fd79910f-4d11-41b4-9e53-198fff089917",
"permanentLocationId": "cee4d952-da5a-4d34-bb0a-a5d4d4581f39"
}');
insert into test (text) values ('{
"id": "aa1e24cd-2825-490f-8ccb-60bd4914dc20",
"circulationNotes": [
{
"id": "3794824a-b814-4b94-b7c4-d8be53088a51",
"note": "REFERENCE BOOK",
"noteType": "Check in"
},
{
"id": "175989f9-16f4-4cfe-a1aa-d665ec1263e7",
"note": "REFERENCE BOOK",
"noteType": "Check out"
}
],
"holdingsRecordId": "fd79910f-4d11-41b4-9e53-198fff089917",
"permanentLocationId": "cee4d952-da5a-4d34-bb0a-a5d4d4581f39"
}');
Then, to solve the issue I split the problem in 2 steps: decomposing and recomposing.
Step 1: create a table at circulationNotes and place the new id column with the following
with fixing_id as (
select id,
text->'id' json_id,
text->'holdingsRecordId' holdingsRecordId,
text->'permanentLocationId' as permanentLocationId,
('{"id":"'||uuid_generate_v1()||'"}')::jsonb || jsonb_array_elements((text->'circulationNotes')::jsonb) as circulationNotes
from test
where exists
(select *
FROM jsonb_array_elements(text->'circulationNotes')
where value->'note' is not null
and value->'id' is null))
select * from fixing_id
The result is the following
id | json_id | holdingsrecordid | permanentlocationid | circulationnotes
----+----------------------------------------+----------------------------------------+----------------------------------------+---------------------------------------------------------------------------------------------------
1 | "aa1e24cd-2825-490f-8ccb-60bd4914dc20" | "fd79910f-4d11-41b4-9e53-198fff089917" | "cee4d952-da5a-4d34-bb0a-a5d4d4581f39" | {"id": "4ad151ea-f5c9-11eb-a458-42010af00961", "note": "REFERENCE BOOK", "noteType": "Check in"}
1 | "aa1e24cd-2825-490f-8ccb-60bd4914dc20" | "fd79910f-4d11-41b4-9e53-198fff089917" | "cee4d952-da5a-4d34-bb0a-a5d4d4581f39" | {"id": "4ad155fa-f5c9-11eb-a458-42010af00961", "note": "REFERENCE BOOK", "noteType": "Check out"}
2 | "aa1e24cd-2825-490f-8ccb-60bd4914dc20" | "fd79910f-4d11-41b4-9e53-198fff089917" | "cee4d952-da5a-4d34-bb0a-a5d4d4581f39" | {"id": "4ad15708-f5c9-11eb-a458-42010af00961", "note": "REFERENCE BOOK", "noteType": "Check in"}
2 | "aa1e24cd-2825-490f-8ccb-60bd4914dc20" | "fd79910f-4d11-41b4-9e53-198fff089917" | "cee4d952-da5a-4d34-bb0a-a5d4d4581f39" | {"id": "4ad157a8-f5c9-11eb-a458-42010af00961", "note": "REFERENCE BOOK", "noteType": "Check out"}
3 | "aa1e24cd-2825-490f-8ccb-60bd4914dc20" | "fd79910f-4d11-41b4-9e53-198fff089917" | "cee4d952-da5a-4d34-bb0a-a5d4d4581f39" | {"id": "4ad15870-f5c9-11eb-a458-42010af00961", "note": "REFERENCE BOOK", "noteType": "Check in"}
3 | "aa1e24cd-2825-490f-8ccb-60bd4914dc20" | "fd79910f-4d11-41b4-9e53-198fff089917" | "cee4d952-da5a-4d34-bb0a-a5d4d4581f39" | {"id": "4ad15906-f5c9-11eb-a458-42010af00961", "note": "REFERENCE BOOK", "noteType": "Check out"}
(6 rows)
Check that I'm using the uuid_generate_v1() function to generate the uuid (you'll need the uuid-ossp extension). And that the jsonb_array_elementscreates a row for eachtext->'circulationNotes'` item.
Step 2: Aggregate from the fixing_id with jsonb_agg and rebuild the json object with jsonb_build_object
select id,
jsonb_build_object('id',json_id,'circulationNotes',jsonb_agg(circulationNotes) ,'holdingsRecordId',holdingsRecordId,'permanentLocationId',permanentLocationId)
from fixing_id
group by id,
json_id,
holdingsRecordId,
permanentLocationId
result
id | jsonb_build_object
----+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 | {"id": "aa1e24cd-2825-490f-8ccb-60bd4914dc20", "circulationNotes": [{"id": "b71fc232-f5c9-11eb-a458-42010af00961", "note": "REFERENCE BOOK", "noteType": "Check in"}, {"id": "b71fc39a-f5c9-11eb-a458-42010af00961", "note": "REFERENCE BOOK", "noteType": "Check out"}], "holdingsRecordId": "fd79910f-4d11-41b4-9e53-198fff089917", "permanentLocationId": "cee4d952-da5a-4d34-bb0a-a5d4d4581f39"}
3 | {"id": "aa1e24cd-2825-490f-8ccb-60bd4914dc20", "circulationNotes": [{"id": "b71fc624-f5c9-11eb-a458-42010af00961", "note": "REFERENCE BOOK", "noteType": "Check in"}, {"id": "b71fc6c4-f5c9-11eb-a458-42010af00961", "note": "REFERENCE BOOK", "noteType": "Check out"}], "holdingsRecordId": "fd79910f-4d11-41b4-9e53-198fff089917", "permanentLocationId": "cee4d952-da5a-4d34-bb0a-a5d4d4581f39"}
2 | {"id": "aa1e24cd-2825-490f-8ccb-60bd4914dc20", "circulationNotes": [{"id": "b71fc4b2-f5c9-11eb-a458-42010af00961", "note": "REFERENCE BOOK", "noteType": "Check in"}, {"id": "b71fc55c-f5c9-11eb-a458-42010af00961", "note": "REFERENCE BOOK", "noteType": "Check out"}], "holdingsRecordId": "fd79910f-4d11-41b4-9e53-198fff089917", "permanentLocationId": "cee4d952-da5a-4d34-bb0a-a5d4d4581f39"}
(3 rows)
The whole query is
with fixing_id as (
select id,
text->'id' json_id,
text->'holdingsRecordId' holdingsRecordId,
text->'permanentLocationId' as permanentLocationId,
('{"id":"'||uuid_generate_v1()||'"}')::jsonb || jsonb_array_elements((text->'circulationNotes')::jsonb) as circulationNotes
from test
where exists
(select *
FROM jsonb_array_elements(text->'circulationNotes')
where value->'note' is not null
and value->'id' is null))
select id,
jsonb_build_object('id',json_id,'circulationNotes',jsonb_agg(circulationNotes) ,'holdingsRecordId',holdingsRecordId,'permanentLocationId',permanentLocationId)
from fixing_id
group by id,
json_id,
holdingsRecordId,
permanentLocationId
Related
I have a JSON in the RedShift.
"value" is stored in the inputs column as JSON text. Create a unique record for each "value". Look at the example below this table:
{"inputs": [{"name": "ambient", "desc": "Select from below the ambient setting that best decribe your environment right now", "values": ["Indoor - Loud", "Indoor - Normal", "Indoor - Whisper", "Outdoor - Loud", "Outdoor - Normal", "Outdoor - Whisper", "Semi-Outdoor - Loud", "Semi-Outdoor - Normal", "Semi-Outdoor - Whisper"]}
As a result it must be like this:
ProjectId : 10. Input value = Indoor – Loud
ProjectId : 10. Input value = Indoor – Normal
ProjectId : 10. Input value Indoor – Whisper
Each value need to be stored as one row in the dim_collect_user_inp_configs table. Example, Indoor-Loud as one row and it will have it’s own unique identifier as prompt_input_value_id, Indoor-Normal as one row and it will have it’s own unique identifier as prompt_input_value_id till the Semi-Outdoor-Whisper.
There could be multiple input “name” in one inputs column. Each name and its value need to be stored separately. Example :
[{"desc": "How many people does the video contain?", "name": "Number of People", "type": "dropdown", "values": ["", "Only 1", "2-3", "3+"]}, {"desc": "What is the camera position?", "name": "Movement", "type": "dropdown", "values": ["", "Fixed position", "Moving"]}, {"desc": "From which angle did you shoot the video?", "name": "Shoot Angle", "type": "dropdown", "values": ["", "Frontal recording", "Tight angle: 10-40 degree", "Wide angle: 40-70 degree"]}, {"desc": "From which distance did you shoot the video?", "name": "Distance", "type": "dropdown", "values": ["", "Near/Selfie", "Mid (3-6 ft)", "Far (>6 ft)"]}, {"desc": "What is the video lighting direction?", "name": "Lighting Direction", "type": "dropdown", "values": ["", "Front lit", "Side lit", "Back lit"]}, {"desc": "What is the video background?", "name": "Background", "type": "dropdown", "values": ["", "Outdoors", "In office", "At home", "Plain background"]}, {"desc": "What is the topic in your speech?", "name": "Topic", "type": "dropdown", "values": ["", "Arts and Media", "Business", "Education", "Entertainment", "Food/Eating", "Nutrition", "Healthcare ", "High School Life", "Mental Health", "News", "Technology", "Morals and Ethics", "Phones and Apps", "Sports", "Science"]}]
I try to do it with this query:
WITH all_values AS (
SELECT projectid AS projectid,
prompttype AS prompttype,
json_extract_path_text(json_extract_array_element_text(inputs, 0, True), 'name') AS name,
json_extract_path_text(json_extract_array_element_text(inputs, 0, True), 'desc') AS description,
json_extract_path_text(json_extract_array_element_text(inputs, 0, True), 'values') AS value,
scriptid AS scriptid,
corpuscode AS corpuscode
FROM source.table
WHERE
prompttype = 'input'
GROUP BY projectid, prompttype, name, description, scriptid, corpuscode, value
LIMIT 10
)
SELECT * FROM all_values;
But now I haven't each row for "value" as I need. :(
Can you help me?
Thnx.
I try to parse JSON in RedShift.
My string in column "inputs" is:
[{"desc": "How many people does the video contain?", "name": "Number of People", "type": "dropdown", "values": ["", "Only 1", "2-3", "3+"]}, {"desc": "What is the camera position?", "name": "Movement", "type": "dropdown", "values": ["", "Fixed position", "Moving"]}, {"desc": "From which angle did you shoot the video?", "name": "Shoot Angle", "type": "dropdown", "values": ["", "Frontal recording", "Tight angle: 10-40 degree", "Wide angle: 40-70 degree"]}, {"desc": "From which distance did you shoot the video?", "name": "Distance", "type": "dropdown", "values": ["", "Near/Selfie", "Mid (3-6 ft)", "Far (>6 ft)"]}, {"desc": "What is the video lighting direction?", "name": "Lighting Direction", "type": "dropdown", "values": ["", "Front lit", "Side lit", "Back lit"]}, {"desc": "What is the video background?", "name": "Background", "type": "dropdown", "values": ["", "Outdoors", "In office", "At home", "Plain background"]}, {"desc": "What is the topic in your speech?", "name": "Topic", "type": "dropdown", "values": ["", "Arts and Media", "Business", "Education", "Entertainment", "Food/Eating", "Nutrition", "Healthcare ", "High School Life", "Mental Health", "News", "Technology", "Morals and Ethics", "Phones and Apps", "Sports", "Science"]}]
My task is: "Each value, name, desc from JSON need to be stored as one row in the table".
Example:
id: 1, desc: "How many people does the video contain?"
id: 2, desc: "What is the camera position?"
etc.
I use query:
SELECT c.*, d.desc, d.name, d.values FROM source.table AS c, c.inputs AS d;
And got an ERROR: navigation on column "inputs" is not allowed as it is not SUPER type
And query:
SELECT c.*, d.desc, d.name, d.values FROM source.table AS c, JSON_PARSE(c.inputs) AS d;
gives me another error: "function expression in FROM may not refer to other relations of same query level"
But when I have created test JSON as:
CREATE TABLE test_parse_json_super
(
id smallint,
details super
);
INSERT INTO test_parse_json_super VALUES(1, JSON_PARSE('[{"desc": "How many people does the video contain?", "name": "Number of People", "type": "dropdown", "values": ["", "Only 1", "2-3", "3+"]}, {"desc": "What is the camera position?", "name": "Movement", "type": "dropdown", "values": ["", "Fixed position", "Moving"]}, {"desc": "From which angle did you shoot the video?", "name": "Shoot Angle", "type": "dropdown", "values": ["", "Frontal recording", "Tight angle: 10-40 degree", "Wide angle: 40-70 degree"]}, {"desc": "From which distance did you shoot the video?", "name": "Distance", "type": "dropdown", "values": ["", "Near/Selfie", "Mid (3-6 ft)", "Far (>6 ft)"]}, {"desc": "What is the video lighting direction?", "name": "Lighting Direction", "type": "dropdown", "values": ["", "Front lit", "Side lit", "Back lit"]}, {"desc": "What is the video background?", "name": "Background", "type": "dropdown", "values": ["", "Outdoors", "In office", "At home", "Plain background"]}, {"desc": "What is the topic in your speech?", "name": "Topic", "type": "dropdown", "values": ["", "Arts and Media", "Business", "Education", "Entertainment", "Food/Eating", "Nutrition", "Healthcare ", "High School Life", "Mental Health", "News", "Technology", "Morals and Ethics", "Phones and Apps", "Sports", "Science"]}]'));
and use query "SELECT c.*, d.desc, d.name, d.values FROM test_parse_json_super AS c, c.details AS d;" from official RedShift docs - it's work fine and all data from JSON is parsing to each rows and JSON is correct.
How I can fix query for work with my real data?
Thnx.
It looks like you are confusing the concepts of SUPER data type for sub-select. You cannot cast a string to super (using JSON_PARSE) and then use it as the source of the FROM clause at the same level of the query. I don't have a complete understanding of your situation but I think something like this should get you closer:
SELECT c.*, d.desc, d.name, d.values
FROM (
SELECT *, JSON_PARSE(inputs) AS inputs_super
FROM source.table
) AS c,
JSON_PARSE(c.inputs_super) AS d
;
(This is an off the cuff response to show structure so please forgive any syntax issues)
It will be more correct:
SELECT c.*, d.desc, d.name, d.values
FROM (
SELECT id, created, JSON_PARSE(inputs) AS inputs_super
FROM source.table
WHERE prompttype = 'input'
) AS c,
c.inputs_super AS d
;
I ran into a similar issue. The json has to be parsed in a created table before use, and cannot be referenced via a CTE or subquery and then used. Therefore I believe the answer is:
CREATE TABLE my_table_with_super
AS
(
SELECT c.*,
JSON_PARSE(c.inputs) AS inputs_json_super
FROM source.table c
);
SELECT c.*,
json_row.desc,
json_row.name,
json_row.values
FROM my_table_with_super as c,
c.inputs_json_super as json_row;
I need to remove the dollar ($) sign from the top of the first bar which is showing the values $410K,only we need 410K.
enter link description here
"chart": {
"caption": "Daily Revenue by Order",
"subcaption": "Last 2 weeks",
"xaxisname": "Date",
"yaxisname": "Revenue (In USD)",
"numberprefix": "$",
"showvalues": "1",
"theme": "fusion"
},
"data": [{
"label": "Total Order",
"value": "410000"
},
Yes, you need to remove numberPrefix attribute. Using this attribute, you could add the prefix to all the numbers visible on the graph.
Hence the modified code will be following.
"chart": {
"caption": "Daily Revenue by Order",
"subcaption": "Last 2 weeks",
"xaxisname": "Date",
"yaxisname": "Revenue (In USD)",
"showvalues": "1",
"theme": "fusion"
},
"data": [{
"label": "Total Order",
"value": "410000"
},
Let's say I have json in the database of items:
Row1:
{"Id": "1", "Items": [{"Item": "Test Item", "Price": "$5.00"}, {"Item": "Test Item #2", "Price": "$15.00"}]}
Row2:
{"Id": "2", "Items": [{"Item": "Test Item #3", "Price": "$1.00"}, {"Item": "Test Item #1", "Price": "$4.00"}]}
How do I get rows formatted like this (| is column seperator):
1 | Test Item, Test Item #2
2 | Test Item #3, Test Item #1
SELECT ID || '|' || ARRAY_TO_STRING( ARRAY_AGG( ITEMS ), ', ')
FROM
(
SELECT T.J->>'Id' AS ID, json_array_elements((T.J->'Items')::json)->>'Item' AS ITEMS
FROM
(
SELECT ('{"Id": "1", "Items": [{"Item": "Test Item", "Price": "$5.00"}, {"Item": "Test Item #2", "Price": "$15.00"}]}')::json AS J
UNION all
SELECT ('{"Id": "2", "Items": [{"Item": "Test Item #3", "Price": "$1.00"}, {"Item": "Test Item #1", "Price": "$4.00"}]}')::json AS J
) T
) T
GROUP BY ID
I ran _">https://graph.facebook.com/act_47121778/adcreatives?access_token=_
and got the following results. Why are some creatives returning empty image hash?
{
"view_tag": "",
"alt_view_tags": [
],
"creative_id": "6002716206572",
"type": 1,
"title": "Grow your fans!",
"body": "Fan Page Owners! Grow your fanbase. Create quizzes for your page. Win $100 Weekly. Make money with our revenue share program.",
"image_hash": "6ac30e43f21c8580361de92d3288ed68",
"link_url": "http://www.facebook.com/quizwriter?sk=app_81434956828",
"name": "Grow your fans!",
"run_status": 1,
"preview_url": "http://www.facebook.com/ads/api/creative_preview.php?cid=6002716206572",
"count_current_adgroups": 1,
"id": "6002716206572",
"image_url": "http://creative.ak.fbcdn.net/v41818/flyers/109/44/1296329249810543669_1_31eb15ed.jpg"
},
{
"view_tag": "",
"alt_view_tags": [
],
"creative_id": "6002444043572",
"type": 1,
"title": "Tennis Champs Social Game",
"body": "Tennis Champ! Start with 5,000 dollars , beat out other players with your mad tennis skills and become the tennis champ! Socialize!",
"image_hash": "",
"link_url": "334886511760",
"name": "Tennis Champs Social Game",
"run_status": 1,
"preview_url": "http://www.facebook.com/ads/api/creative_preview.php?cid=6002444043572",
"count_current_adgroups": 2,
"id": "6002444043572"
},