Extracting data from a JSON format in Flask is a crucial task that can be

Can someone please help me with extracting data from JSON format? I am trying to create a search input in JSON and then retrieve information from MYSQLdb. Here is the data I have:

{"result":[[21,"bogdan333","bogdan333",0,"","templates/UPLOAD_FOLDERp7.jpg","",1],[22,"Bogdan3333","Bogdan3333",0,"","profile_pic/p12.jpg","back_pic/p5.jpg",1]]}

I am struggling to extract just the names "Bogdan3333." Here is my code:

<script type="text/javascript">
function getState3(value){
    $.post("/search_index?medic={{session['medic']}}&id={{id_general_user}}&connect=connect",{searching:value},function(data2){
        $(".solutii").html(data2);
    });
}
</script>
 <form id="custom-search-form" method="POST" class="form-search form-horizontal pull-right" action="">
                <div class="input-append span12">
                    <input type="text" class="search-query mac-style" onkeyup="getState3(this.value)"  ng-model="firstName" placeholder="Search">

                    <div class="finder-search"><p class="firstName"> {{ firstName }}</p></div>

                    <button type="submit" class="btn"><i class="icon-search"></i></button>
                </div>
            </form>

And this is the relevant Flask part of the code:

@app.route('/search_index',methods=['POST','GET'])
def search_index():
    if request.method=="POST":
        search=request.form['searching']
        sql_search="SELECT * FROM register WHERE medic=1 AND uname LIKE '%"+str(search)+"%' LIMIT 20"
        conn=con.execute(sql_search)
        print(conn)
        get=con.fetchall()
        print(get)
##        return redirect(url_for("index",medic=session['medic'],id=session['id'],connect='connect'))
        return jsonify(result=get)

Answer №1

To extract information from a JSON file, it is essential to transform the text into a suitable data structure. In order to achieve this, you can utilize Python's built-in json module.


import json
data = json.loads(json_string)

# Accessing the desired data
result = data['information']

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

The function json_decode() in PHP fails to handle special characters like accents

When working with a JSON array that contains accented characters, such as üna, I use json_encode($array, JSON_UNESCAPED_UNICODE); to save it correctly. However, when trying to display this on a page using json_decode(), the accent causes the value not to ...

PHP is used to download JSON data in mobile applications

My iOS and Android app is designed to download data from a database using JSON and PHP. The process involves numerous mysql queries that retrieve information from my MySQL database. Initially, I created an array in PHP to store all the queries and would ac ...

Using a jQuery calendar to generate a JSON date range

In my current situation, I am faced with a task where I need to choose a date from a jQuery Calendar and then, using ajax, cycle through a JSON file. The problem I encounter stems from dealing with date ranges, such as startdate and enddate: { "campu ...

When submitting the form, does it trigger the opening of the PHP file?

Let's get straight to the point. Before anything else, I want to say THANKS IN ADVANCE. So, here's what's happening: When I submit the form I created, it goes through the submission process and all, but then it redirects the page to the PHP ...

Navigate through the JSON dataset

I am struggling with looping out JSON-data in an HTML list using PHP. The structure of the JSON data is as follows: { "msg": [ "msg text 1", "msg text 2", "msg text 3", "msg text 4", "msg text 5", "msg text 6" ] } My current P ...

Encountering an issue when attempting to access a string offset while creating a JSON object in PHP

After countless attempts and failed solutions, I find myself struggling to make this work. The scenario is as follows: I have an initially empty file named contact.json. If the file is empty, I need to add JSON data to it. If it already contains data, I ...

Use PHP shopify_call to sync and adjust the inventory in your Shopify store

I am in the process of developing a PHP-based API for Shopify to facilitate updating the inventory_quantity of a product. Below is the JSON representation of my product: "variants": [ { "id": 1234567890123, "produc ...

Populating a dropdown box in MySQL database using PHP and implementing a search function

Hi there, I have a question about creating a search feature on a webpage using a textbox and a dropdown box. The table I am working with has the following fields: Occupation field) and allows users to input additional search criteria in a textbox, return ...

What is the most efficient method for retrieving information from this JSON using PHP?

[{"id":"1",name":"Title One","players":"999"},{"id":"2","name":"Title Two","players":"100"}] What is the best way to display this information? ...

The ID attribute of Laravel blade file input is missing

Within my blade file, I have the following line of code: {!! Form::file('motivation', old('motivation'), ['id' => 'inputGroupMotivation', 'class' => 'custom-file-input']) !!} Although I ha ...

Can multiple sets of data be stored in a single column within a MySQL database?

Is it possible to post two datas in one table? Can a column handle multiple data entries at once? Check out the modal here For example, I need to list two monitors for my unit. However, when checking the database, only the first field seems to get record ...

Transferring JSON data from an iOS device to a PHP server

I've run into an issue while attempting to transmit JSON data from an iOS application to a mySQL database by means of a php page. Unfortunately, it seems that the POST data is not being received on the php page. - (IBAction)jsonSet:(id)sender { ...

Moving the query function from routes to the repository in Laravel

Greetings! I am relatively new to the world of Laravel and I am in the process of setting up a repository to consolidate repeated database calls. It seemed logical to centralize these calls for easier reference. Currently, I have a chained select feature t ...

Issue with FILE_APPEND and file_put_contents function not functioning as expected

My approach involves sending form data as JSON via AJAX to a PHP file, which then saves the JSON information in a text file on the server. An issue arises when using FILE_APPEND, as the data is not written to the file if the JSON content already exists wi ...

Retrieve the JSON encoded output from the Controller and pass it to the View

I am trying to convert a json encoded result from ManagementController.php, specifically the statisticAction, into Highchart syntax within the view file (stat.phtml) in my Phalcon project. Originally, in the stat.phtml file, I had the following: ...

Using Guzzle to Make a JSON POST Request in Laravel 5.7

I need help with my code. I am trying to create a new store in Geoserver. public function post_store(String $name) { $client = new Client(); $res = $client->request('POST', 'http://localhost:8080/geoserver/rest/workspaces/&a ...

Tips for transferring custom data on a form without relying on input fields are an effective way

Is there a way to submit a form with both name and quantity without having an input field for the second variable? I want the quantity get variable to be passed when the form is submitted, but I also need to pass the name quantity that is already displayed ...

Utilizing AJAX and PHP for dynamic changes based on listbox selection

Is there a way to make the query run whenever a selection is made from the listbox? if(isset($_POST['select'])){ $newselection=$_POST['select']; $query = "SELECT count(*) FROM listings where description=" . $newselection. ""; $r ...

If PHP does not return data in a JSON encoded format, Ajax will not function properly

I have a PHP script that returns an array if an error occurs, otherwise it returns a <DIV> if(!empty($error)) { $true = true; $res = array('info' => '$error', 'error' => '$true'); echo json_enc ...

Prevent PHP from escaping JSON string when saving to a file

I am facing an issue while trying to save changes to a JSON file using JQuery and PHP. It seems that my PHP script is escaping characters when saving out the JSON, making it unreadable. The following code snippet passes the JSON object ('family' ...