"SQL Noob Alert: Help Needed to Retrieve Data from Nested JSON Objects"

678ka

New member
Joined
Nov 5, 2008
Messages
2
Reaction score
0
Title: SQL Noob Alert: Help Needed to Retrieve Data from Nested JSON Objects

Hey guys, I'm new to SQL and I'm stuck on something that's probably easy for you veterans. I've got a database with a JSON object containing user info, and it's nested like this: `{ "name": "John", "address": { "street": "123 Main St", "city": "NYC" } }`. I'm trying to write a query to extract the street names, but I'm not sure how to access the nested address object. Can anyone offer some guidance?
 

Daiane Gomes

New member
Joined
Jul 10, 2023
Messages
3
Reaction score
0
"Hey noob no worries! I've been there too - try using a JSON_PATH query or the UNNEST function depending on your SQL flavor. Also, share your SQL code and I can give you a more specific answer"
 

Vov332

New member
Joined
Aug 20, 2017
Messages
2
Reaction score
0
"Lol, I've been in this spot before, bro. Try using the `json_extract` function, or you can use a nested path like `json_extract(col, '$.nestedObject.path')`. Can you post the actual SQL query and your JSON object so we can get a better look?"
 

Voice74

New member
Joined
Jul 25, 2011
Messages
2
Reaction score
0
"Dude, what RDBMS are you using? I'm guessing it's MySQL or PostgreSQL, but if you're using MongoDB, you'd be better off using their native query language. Either way, share the JSON structure and I'll help you out."
 

AntonioReal

New member
Joined
Mar 15, 2012
Messages
3
Reaction score
0
"Lol, nested JSON objects can be a real pain, bro. I'd recommend checking out the `jsonb_array_elements` function in Postgres, it's a lifesaver for parsing nested JSON. Can you share the SQL query you're using and the JSON structure you're dealing with?"
 

LILITH_ALGOL

New member
Joined
Oct 14, 2019
Messages
1
Reaction score
0
"Hey OP, I'm no pro either, but I've had some success using the `JSON_EXTRACT` function in MySQL. Can you give me an idea of what's in your JSON object and what you're trying to pull out? Maybe we can brainstorm together."
 

icsapfir

New member
Joined
Dec 8, 2004
Messages
3
Reaction score
0
"dude, you can use JSON_EXTRACT to get specific values from nested JSON objects. For example, something like this: `SELECT *, JSON_EXTRACT(data, '$.nested_obj->>value') AS nested_value FROM table`."
 

Arsenikum_prk

Member
Joined
May 27, 2010
Messages
5
Reaction score
0
"Yo, I've had to deal with nested JSONs before when working with blockchain data. Usually, you can just use the `->>` or `->` operator to extract the value you need. Can you post your SQL query so we can take a closer look?"
 
Top