Choosing an Array of Integers in PostgreSQL: A Guide

My goal is to extract arrays of integers from a table in this format:

[1, 2, 3]

I attempted the following query:

(SELECT array_to_json(array_agg(row_to_json(s))) FROM(
 SELECT specialty FROM talent_specialty WHERE userid = 840 )s);

This is the result returned by the query:

[{"specialty":1},{"specialty":2}]

The structure of the table is as follows:

https://i.stack.imgur.com/EujSI.png

Answer №1

Looking to find the json_agg function?

Check out this demo on db<>fiddle

SELECT json_agg(speciality) 
FROM talent_speciality

If you prefer a standard array over a JSON one, you can use the array_agg function instead.

SELECT array_agg(speciality) 
FROM talent_speciality

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using Python's for loop to iterate through a two-dimensional index

I'm facing a challenge that seems simple, but I'm struggling to figure out how to tackle it using Python. Within my Python for loop, I have a unique value defined during each iteration. Now, I want to access the value of the NEXT or PREVIOUS uni ...

jqGrid JSON Parsing Issue

While utilizing jqGrid and JSON server responses, I am facing an issue with correctly mapping my JSON data. For instance, the server response appears as follows: [ {ID: 'cmp1', Name: 'Name1', Address: 'Address1', Phone: ...

Retrieve a specific nested key using its name

I am working with the following structure: const config = { modules: [ { debug: true }, { test: false } ] } My goal is to create a function that can provide the status of a specific module. For example: getStatus("debug") While I can access the array ...

Sorting for the best price in Java using Selenium

I am currently using selenium to automate the process on makemytrip.com. My main goal is to identify the lowest fare available in the list of options provided. Below is the code I am utilizing: List<WebElement> results = driver.findElements(By.xpa ...

Eliminate duplicate entries from various JSON Arrays

I am facing an issue with a JSON structure containing multiple arrays. My goal is to identify and eliminate any duplicate values across different arrays while keeping the other fields intact. Here is a sample of the structure I am working with: { "Coll ...

What is the best way to convert a circular JSON object to a string

Is there a way to stringify a complex JSON object without encountering the "Converting circular structure to JSON" error? I also need its parser. I am facing issues every time I try to use JSON.stringify and encounter the "Converting circular structure to ...

Extract only the values from a JSON string using Regular Expressions

Need help cutting out specific values from a JSON String in Flutter/Dart. Here's an example of the JSON String: [{"insert":"Test12\n"},{"insert":"Test1","attributes":{"b":true}},{&quo ...

How can labels be added when mapping over JSON data?

If I have JSON data structured like this: { "siteCode": "S01", "modelCode": "M001", "modelDesc": "Desc01", "price": 100 "status": "A", "startDate": "Ma ...

Working with JSON data in Java and displaying it in a user interface

As a newcomer to JAVA, I find myself facing JSON data for the first time. My task is to develop a news desktop application by sending a GET request to a web address that returns JSON output, as illustrated below. ****JSON OUTPUT**** { "status": ...

How to use regular expressions to retrieve the ID of the object with the status "waiting" in a JSON array

Looking to extract the ID of an object from a JSON response using regular expressions. The JSON response contains an array of objects as shown below: ... { "Id":"01", "Subject":"Sub", .... "Status":"Completed" ... }, { "Id":"02", "Subject":"Sub", ...

ListException: 'list' object does not support the 'get' operation

This is the code snippet: def validate_record_schema(record): device = record.get('Payload', {}) manual_added= device.get('ManualAdded', None) location = device.get('Location', None) if isinsta ...

Acquiring mapping information through JSON data retrieval

Overview : Each levelNum has three corresponding dropdowns. For example, the dropdown for levelNum2 includes (Department-Unit-1, Department-Unit-2 & Department-Unit-3), levelNum3 includes (Division-Unit-1 & Division-Unit-2), and levelNum4 includes ...

Tips for extracting values from an array that was fetched using cURL and stored as JSON with .JQ

{ "accounts": [ { "accountId": "account001", "tokens": [ "tokens1", "tokens2", "tokens3", "tokens4", ] } ] } the ...

The media parameter seems to be malfunctioning when attempting to send it to the Kaleyra API using code

Attempting to send media through the Kaleyra API using my code is proving unsuccessful. However, when I make the same request via Postman, it works perfectly fine. async whatsappAPIWithAttachment(requestBody) { let api_key = ""; if (requ ...

What is the most efficient method for storing and retrieving numerous DOM elements as JSON using the FileSystem (fs) Module in Node.js?

Is there a way to efficiently save dynamically added DOM elements, such as draggable DIVs, to a file and then reload them later on? I am looking for the most organized approach to achieve this. ...

Following the mongoimport process, the MongoDB database does not display any collections

Recently, I delved into the world of MongoDB and mapReduce, but encountered a hurdle along the way. The MongoDB installation went smoothly. Next, I attempted to import a json file by executing these 2 commands before launching mongo in the terminal (worki ...

Grin schedule module JSON stream

I have integrated a Smile timeline widget on my website and successfully customized it following several tutorials. However, I am struggling to utilize a Json service instead of relying on data stored in a global variable within a JavaScript file. Despite ...

Having trouble retrieving JSON data using jQuery and Handlebars?

Hey there, I'm new to JSON and all the talk about JavaScript templating engines for front-end dynamic changes. However, I'm struggling to make progress. Can anyone help me out? The JSON file is named data.json { users: [{ userna ...

Any tips on how to convert a bincode serialized data into serde_json::Value?

I'm currently working on an IPC implementation where a process serializes a struct using bincode. However, on the receiving end, the process is unaware of the structure it's receiving. I want to achieve something similar to: let parsed: Result&l ...

Iterate over a JSON array using jQuery

I am attempting to iterate through this data to extract the 'name' values. Here is my current code, but it does not seem to be functioning correctly. I have tried a few variations from suggestions here, but none of them have worked. $.get("/ ...