Sunday, November 9, 2025
HomeBig DataFlattening a JSON Object So It’s Queryable Utilizing Rockset

Flattening a JSON Object So It’s Queryable Utilizing Rockset

[ad_1]

Many builders use NoSQL databases so as to ingest unstructured and schemaless knowledge. In the case of understanding the info by writing queries that be a part of, combination, and search, it turns into tougher. That is the place Rockset turns into an excellent accomplice not solely in understanding your unstructured knowledge however in returning queries that be a part of, combination, and search inside milliseconds at scale. Rockset is a real-time indexing database constructed for the cloud that acts as an exterior indexing layer on high of your knowledge lakes, knowledge streams, transactional databases, and knowledge warehouses.


flattening json objects

On this twitch stream, we created a MongoDB Atlas occasion. After the occasion is created, you could have the choice to make use of the MongoDB preseeded databases. Right here I used the database referred to as netflix and the gathering referred to as films.


snapshot mongodb

After we configure the occasion, we created an integration on Rockset with MongoDB, by utilizing the built-in knowledge connector for MongoDB. We offer restricted credentials, so Rockset can learn the info from MongoDB. The directions to configure Atlas and create the Rockset integration might be discovered right here — or you possibly can watch the stream beneath!

Inspecting the info

As soon as the info is in Rockset, it should look one thing like this:

Embedded content material: https://gist.github.com/nfarah86/ef1cc9da88e56226c4c46fd0e3c8e16e

For those who observed the sphere genres appears to be like like this:

"genres": "[{'id': 80, 'name': 'Crime'}]"

… Strings, Strings, in every single place…


strings-everywhere

Mainly, we’ve a string sort as a price, when it ought to be an array of objects. Let’s say you needed to see all of the style’s names with out the id key; you wouldn’t be capable of write a question that may do that, because it’s at the moment formatted.

Reworking Genres from a JSON String → to an ARRAY


transforming-genres

Rockset has a operate referred to as UNNEST, that can be utilized to increase array of values or paperwork to be queried (aka flattening the JSON object). Assuming no errors in how genres is formatted as a string, we will accomplish this in 2 steps:

  1. Parse the given string as JSON:

Right here, you should use JSON_PARSE, which parses a given JSON string as a JSON object:

SELECT JSON_PARSE("[{"id":3, "name":"thriller"}]");

While you run that within the Question Editor, you must get this again:

-- get an array of objects again
[{"id":3, "name":"thriller"}]

Be mindful, our string is at the moment formatted like this:

“[{'id': 80,'name': 'Crime'}]"

  1. Broaden the array and flatten the JSON object:

Use UNNEST:

SELECT
genres.worth.title
FROM
yourCollectionName,
UNNEST(yourCollectionName.genres AS worth) AS genres
GROUP BY
genres.worth.title
;

While you run this question, you must get:

-- results of UNNEST the place we return genres.title
[{"name": "Crime”}]

Within the following recorded twitch stream, we truly acquired a curveball ball 🎾, the place we couldn’t JSON_PARSE(genres). A parsing error was thrown as a result of the string within the knowledge is malformatted. On this case, we added an additional step to resolve this. Take a look at the stream 👇 to see how we resolved the error– (and don’t overlook to comply with us!)

Embedded content material: https://www.youtube.com/watch?v=AtCcXrtgQCg&record=PLinxPR8yVNSLjolgZUr1XU9VAoES3iMuX&index=7

TLDR: yow will discover all of the sources you want to get began on Rockset within the developer nook.



[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments