Search and extract JSON data in MySQL

Currently, I am inputting JSON objects into a MySQL Database and then executing queries on them. Within the database is a table structured as follows:

subjects                                                |
---------------------------------------------------------
|{
|"subjects": [{
|  "subject":"English Language & Literature",
|  "ageRange":["12-18"],
|  "examlevel":["13+","gcse","as-level","a-level"],
|  "examboard":["aqa","edexcel","ocr","wjec"]
|},
|{
|  "subject":"Creative Writing",
|  "ageRange":["8-18"],                                     | Row 1
|  "examlevel":null,
|  "examboard":null
|},
|{
|  "subject":"Media Studies",
|  "ageRange":["14-18"],
|  "examlevel":["gcse","as-level","a-level"],
|  "examboard":["aqa","edexcel","ocr","wjec"]
|}]
|}
---------------------------------------------------------

In one of my MySQL queries, I executed

SELECT JSON_EXTRACT(subjects, '$."subjects"') FROM `table_name`

This returned

[{"subject": "English Language & Literature", "age...

To extract the subject name, I ran the following...

SELECT JSON_EXTRACT(subjects, '$."subjects[*]."subject"') FROM `table_name` 

The result was

["English Language & Literature", "Creative Writin...

I then attempted to run a search query against the extracted JSON data

SELECT JSON_EXTRACT(subjects, JSON_UNQUOTE(JSON_SEARCH(subjects, 'all', '{Search Query i.e "E"}', NULL, '$."subjects"[*]."subject"'))) FROM `table_name`

This ^^ resulted in NULL

My goal is to dive deeper into querying. I aim to extract the subject name from each JSON object for all subjects within this group. Essentially, the search should iterate through each subject field in every JSON Object for all rows to yield results matching the search query.

Despite consulting the MySQL Docs, the information provided has not been very helpful... Now that I have obtained the Extracted JSON data, how can I perform searches against them? Furthermore, how can I selectively retrieve only those matching the search query?

Answer №1

After analyzing the feedback provided, I have discovered effective solutions to the problem at hand.

The following query will retrieve the columns with an exact match to the specified string:

SELECT JSON_EXTRACT(subjects, JSON_UNQUOTE(JSON_SEARCH(subjects,'all','Creative Writing', NULL, '$."subjects"[*]."subject"'))) FROM `table_name`

--

SELECT JSON_EXTRACT(subjects, JSON_UNQUOTE(JSON_SEARCH(LOWER(subjects),'all',LOWER('%eng%'), NULL, '$."subjects"[*]."subject"'))) FROM `table_name`

This query will fetch the columns containing the characters 'eng' in a case-insensitive manner, ensuring all relevant rows are retrieved.

--

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

Continuous Scrolling with Callback Function in jQuery

I have implemented the infinite-scroll plugin, which replaces traditional pagination with ajax to fetch new pages. The issue I am facing is that jQuery functions do not recognize the new posts, causing certain functions like the ones below to stop working ...

Tips for utilizing an npm package in conjunction with Hugo

I created a basic hugo site with the following command: hugo new site quickstart Within the layouts/_default/baseof.html file, I have included a JavaScript file named script.js. Inside script.js, the code looks like this: import $ from 'jquery' ...

"Guidelines for implementing a post-login redirection to the homepage in React with the latest version of react-router (v

I am facing an issue where I am unable to redirect to the Home Page when I click the "Login" button during my React studies. Despite trying all possible methods for redirects, none of them seem to work. The function that is executed when I click the "logi ...

How can I use jQuery to switch the positions of two <div> elements in HTML based on the selection of a dropdown value?

I need to switch the display positions of two <div> containers based on a dropdown value change. Can this be accomplished using jQuery? Here are the two div containers whose display positions need to be interchanged: <div id="first"><p> ...

How big is the array size in the WebAudio API data?

Exploring the visualization of waveform and FFT generated by the audio stream from the microphone through the WebAudio API. Curiosity strikes - what is the size of each data array available at a given moment? Delving into the getByteTimeDomainData, it men ...

Struggling to display AJAX GET results on my webpage, although they are visible in the Google Element Inspector

I'm working on a basic invoice page where I need to populate a dropdown box from my MySQL database. The issue I'm facing is that when I select an item, the description box doesn't get prepopulated as expected. I've checked in the networ ...

Tips for dynamically changing the field name in a JSON object using Spark

Having a JSON log file with a JSON delimiter (/n), I am looking to convert it into Spark struct type. However, the first field name in every JSON varies within my text file. Is there a way to achieve this? val elementSchema = new StructType() .add("n ...

AngularJS initiates an XMLHttpRequest (XHR) request before each routeChange, without being dependent on the controller being used

I'm currently embarking on a new project, and for the initial phase, I want to verify if the user has an active session with the server by sending an XHR HEAD request to /api/me. My objective is to implement the following syntax $rootScope.$on("$rou ...

The powerful combination of Newtonsoft JSON and Graph API

Utilizing Newtonsoft JSON, I deserialize responses from the Facebook Graph API. For instance, when parsing user posts, the response looks like this: data": [ { "story": "", "created_time": "", "id": "" }] To handle this data, I ...

Converting JSON data into byte format

I'm facing an issue where my code seems to loop back on itself. The problem arises when I create a JSON element with file paths that contain special characters. Encoding the paths results in unicode-escaped characters, making the path unreadable for t ...

A guide on customizing column names in MUI Datatables through object keys

I'm currently facing an issue where I need to set the name of a column in MUI Datatables using an object key. Specifically, I want to set one of the column names with the first element of children.childName so that it displays a list of child names, b ...

Cancel your subscription to a PubNub channel when the unload event occurs

Currently, I am developing a multiplayer game in Angular utilizing the PubNub service along with the presence add-on. One of the challenges I am facing is detecting when a player unexpectedly leaves the game. This is crucial for sending notifications to o ...

Several directories for viewing in Node.js with Express

I have been researching different solutions, but I am still unsure about how to effectively integrate Express with multiple view folders. Imagine an Express application divided into distinct parts, each organized in its own subfolder: app +partA + ...

Trouble with radio button selection in Pyppeteer, puppeteer, and Angular JS

I am struggling to select the 'fire' option in a radio button within a div element using Pyppeteer. Despite multiple attempts, I have not been successful. Here is the structure of the div with the radio button: <div _ngcontent-xqm-c396=" ...

Analyzing JSON data and creating a tailor-made array

My Dilemma { "rowId": "1", "product_name": [ "Item A", "Item B", "Item C", "Item D", "Item E" ], "product_tag": [ "123456", "234567", "345678", "456789", "5678 ...

How can I detect if the browser's built-in object is available in Angular?

Exploring the Wechat JS API: The Wechat JS API relies on the WeixinJSBridge object within the Wechat built-in browser. This object is not immediately available upon opening the WebView; the client-side needs to initialize it. Once the WeixinJSBridge object ...

Basic Node.js messaging application excluding the use of socket.io

Recently, I've delved into learning Node.js and embarked on creating a basic chat application. It appears that socket.io is the go-to option for most developers, but I'm keen on grasping the concept from a more foundational standpoint using GET a ...

What is the most effective method for generating dial images through programming?

Currently, I am in the process of creating a website that makes use of variously sized and styled dials to indicate progress. The filled portion of the dial represents how close an item is to being 100% complete. I am seeking a universal solution that wil ...

Centered iframe within a div

I am trying to center my iframe within a white box and need some assistance with this. I want the iframe to be in the middle of the white box rather than its current position at the moment. game1.html: <html> <head> <title>R ...

Monitoring separate upload progress within $q.all() in AngularJS

I recently started using the angular-file-upload module created by danialfarid (https://github.com/danialfarid/angular-file-upload) and I must say, it's been a great experience so far. After successfully integrating it into my wrapper service for RES ...