Spatial Analysis in 2025: Key Trends Report
| Download Now
CARTO
Platform
Overview
Visualization
Analytics
App Development
Data Enrichment
AI Agents
Enterprise & Security
Pricing
CARTO for
Google Cloud
AWS
Azure
Snowflake
Databricks
Oracle
Solutions
By Industry
By Use Case
By Role
Telecoms
Icon/indus/Insurance-menu
Icon/indus/Insurance-menu-mobile
Insurance
Logistics
Real Estate
Financial Services
Retail
Marketing & Advertising
Mobility
All industries
Network Deployment
Catastrophe Modeling
Fraud Detection
Market Analysis
Environmental Management
Site Selection
Geomarketing
Data Monetization
IoT Analytics
Supply Chain Optimization
Healthcare Analytics
Territory Planning
All use cases
Data Analyst
Data Scientist
GIS Professional
Developer
Customers
Resources
Learn
Documentation
CARTO Academy
Blog
Reports
Glossary
Connect
Events
Partners
Webinars
Log inRequest a demo
Try for free
2050-01-01
1990-01-01
"ES", "GB"

This content applies to a previous version of CARTO

In October 2021 we released a new version of our platform. You can find the latest documentation at docs.carto.com

Back to Help Center

How to Convert Arrays to JSON Objects

Learn how to convert arrays to json objects to display their data in Builder popups.

CARTO supports json (and jsonb) data types. The json_agg PostgreSQL function allows aggregating information in json objects.

SQL Query Example

SELECT wb.cartodb_id,
       wb.name as country,
       wb.the_geom,
       json_agg(
           row_to_json(
               ( SELECT r
                   FROM ( SELECT pp.name as city,
                                pp.pop_max as population
                        ) r
               ),
               true
           )
       ) as json
  FROM world_borders wb
  JOIN populated_places pp
    ON wb.name = pp.adm0name
GROUP BY wb.cartodb_id

Result Table

The query produces rows with aggregated JSON data:

cartodb_idthe_geomcountryjson
166PolygonPortugal[{“city”:“Coimbra”,“population”:106582},{“city”:“Aveiro”,“population”:54162},…]

Custom Popup Implementation

Display this aggregated data using MustacheJS in a custom popup. Assign the json object using {{#json}} and iterate through items to extract city and population values:

<div class="cartodb-popup header blue v2">
  <a href="#close" class="cartodb-popup-close-button close">x</a>
  <div class="cartodb-popup-header">
    <h1></h1>
    <span class="separator"></span>
  </div>
  <div class="cartodb-popup-content-wrapper">
    <div class="cartodb-popup-content">
      <h4>Cities</h4>
      <ul style="list-style-type: disc; list-style-position: inside; ">
      {{#json}}
      <li>
        <ul style="margin-left:5px;list-style-type: circle; list-style-position: inside; ">
          <li>Population: </li>
        </ul>
      </li>
      {{/json}}
      </ul>
    </div>
  </div>
  <div class="cartodb-popup-tip-container">
  </div>
</div>

Note: In dataset view, [Object Object] values will display in cells for json fields.

CARTO
carto-logo
  • Platform

  • Overview

  • Visualization

  • Analytics

  • App Development

  • Data Enrichment

  • Security & Governance

  • Gen AI

  • Pricing

  • Solutions

  • Data Analyst

  • Developer

  • Data Scientist

  • GIS Professional

  • By Industry

  • By Use Case

  • Resources

  • Customer Stories

  • Blog

  • Glossary

  • Documentation

  • Academy

  • Reports

  • Events

  • Webinars

  • Partners

  • Company

  • About us

  • Newsroom

  • Careers

  • Brand

  • Grants

Unlock the power of spatial analysis.

youtube logofacebook logotwitter logolinkedin logo

© CARTO 2024

Terms

Privacy Notice

Whistleblower Form