[Python] Import a JSON file in Python and recall all the data [closed]

Discussão em 'Python' iniciado por Stack, Abril 16, 2025.

  1. Stack

    Stack Membro Participativo

    I would like to import a JSON file that contains 12 thousand rows and search its data one by one by creating an application that allows me to find every precise data I am looking for in the JSON file.I have create the file and used flask to create the web application, then I have tested it and when I try for example a type of key gave me all the information of the different key that are in the json file, but I want to search for example only about this 000 key in MsgSettings, what I have to do?

    MsgSettings": {
    "Messages": {
    "000": {
    "Background": 0,
    "Color": "neutral",
    "Symbol": 0,
    "Type": 0,
    "xPos": 10,
    "yPos": 10


    Reserch on the File JSON Results for "000":

    1000031608
    0003-0000-0092
    1000031012
    1000031012
    1000031012
    1000031800
    0008-0000-0116
    00_000000065
    ('000', {'Background': 0, 'Color': 'neutral', 'Symbol': 0, 'Type': 0, 'xPos': 10, 'yPos': 10})
    ('000', True)
    ('000', True)
    ('000', True)
    ('000', True)
    ('000', {'Max': 4, 'Min': 0})
    2000
    5000
    60000
    1000
    ('000', {'Max': 60000, 'Min': 0})
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    ('000', {'Max': 1, 'Min': 0})
    60000
    60000
    20000
    10000
    60000
    1000
    1000
    2000
    1000
    ('000', {'Max': 60000, 'Min': 0})
    60000
    60000
    60000
    60000
    60000
    30000
    60000
    2000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    1000
    60000
    60000
    60000
    30000
    5000
    1000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    60000
    ('000', 0)
    ('000', 0)
    Prog000
    ('000', 0)
    ('000', 0)
    4000
    ('000', 20)
    10000
    10000
    1000
    1000
    ('000', 0)
    ('000', 0)
    4000
    ('000', 20)
    10000
    10000
    1000
    ('000', 0)
    ('000', 0)
    4000
    ('000', 20)
    10000
    10000
    1000
    ('000', 0)
    ('000', 0)
    4000
    ('000', 20)
    10000
    10000
    1000
    ('000', 0)
    ('000', 0)
    4000
    ('000', 20)
    10000
    10000
    1000
    ('000', 0)
    ('000', 0)
    4000
    ('000', 20)
    10000
    10000
    1000
    ('000', 0)
    ('000', 0)
    4000
    ('000', 20)
    10000
    10000
    1000
    ('000', 0)
    ('000', 0)
    4000
    ('000', 20)
    10000
    10000
    1000
    ('000', 0)
    ('000', 0)
    4000
    ('000', 20)
    10000
    10000
    1000
    ('000', 0)
    ('000', 0)
    4000
    ('000', 20)
    10000
    10000
    1000
    ('000', 0)
    ('000', 0)
    4000
    ('000', 20)
    10000
    10000
    1000
    ('000', 0)
    ('000', 0)
    4000
    ('000', 20)
    10000
    10000
    1000
    ('000', 0)
    ('000', 0)
    4000
    ('000', 20)
    10000
    10000
    1000
    ('000', 0)
    ('000', 0)
    4000
    ('000', 20)
    10000
    10000
    1000
    ('000', 0)
    ('000', 0)
    4000
    ('000', 20)
    10000
    10000
    1000
    ('000', 0)
    ('000', 0)
    4000
    ('000', 20)
    10000
    10000
    1000
    ('000', 0)
    ('000', 0)
    4000
    ('000', 20)
    10000
    10000
    1000
    ('000', 0)
    ('000', 0)
    4000
    ('000', 20)
    10000
    10000
    1000
    ('000', 0)
    ('000', 0)
    4000
    ('000', 20)
    10000
    10000
    1000
    ('000', 0)
    ('000', 0)
    4000
    ('000', 20)
    10000
    10000
    1000
    ('000', 0)
    ('000', 0)
    4000
    ('000', 20)
    10000
    10000
    1000
    ('000', 0)
    ('000', 0)
    4000
    ('000', 20)
    10000
    10000
    1000
    ('000', 0)
    ('000', 0)
    4000
    ('000', 20)
    10000
    10000
    1000
    ('000', 0)
    ('000', 0)
    4000
    ('000', 20)
    10000
    10000
    1000
    ('000', 0)
    ('000', 0)
    4000
    ('000', 20)
    10000
    10000
    1000
    ('000', 0)
    ('000', 0)
    4000
    ('000', 20)
    10000
    10000
    1000
    ('000', 0)
    ('000', 0)
    4000
    ('000', 20)
    10000
    10000
    1000
    ('000', 0)
    ('000', 0)
    4000
    ('000', 20)
    10000
    10000
    1000
    ('000', 0)
    ('000', 0)
    4000
    ('000', 20)
    10000
    10000
    1000
    ('000', 0)
    ('000', 0)
    4000
    ('000', 20)
    10000
    10000
    1000
    ('000', 0)
    ('000', 0)
    4000
    ('000', 20)
    10000
    10000
    1000
    1000


    This is the code

    from flask import Flask, request, render_template_string
    import json
    import pprint

    app = Flask(__name__)

    # Carica il file JSON una volta all'avvio (se il file non cambia frequentemente)
    with open("00_000000065.json", "r", encoding="utf-8") as file:
    DATI_JSON = json.load(file)

    def cerca_dati(query, dati):
    """Cerca la query in tutte le stringhe presenti nel dizionario ricorsivamente."""
    risultati = []

    def ricerca_recorsiva(elemento):
    if isinstance(elemento, dict):
    for chiave, valore in elemento.items():
    if query.lower() in str(chiave).lower():
    risultati.append((chiave, valore))
    ricerca_recorsiva(valore)
    elif isinstance(elemento, list):
    for item in elemento:
    ricerca_recorsiva(item)
    else:
    if query.lower() in str(elemento).lower():
    risultati.append(elemento)

    ricerca_recorsiva(dati)
    return risultati

    # Template HTML semplice
    template = """
    <!doctype html>
    <html lang="it">
    <head>
    <meta charset="UTF-8">
    <title>Ricerca Dati JSON</title>
    </head>
    <body>
    <h1>Ricerca nel File JSON</h1>
    <form method="get">
    <input type="text" name="query" placeholder="Inserisci termine di ricerca">
    <input type="submit" value="Cerca">
    </form>
    {% if query %}
    <h2>Risultati per "{{ query }}":</h2>
    {% if risultati %}
    <ul>
    {% for item in risultati %}
    <li>{{ item }}</li>
    {% endfor %}
    </ul>
    {% else %}
    <p>Nessun risultato trovato.</p>
    {% endif %}
    {% endif %}
    </body>
    </html>
    """

    @app.route("/", methods=["GET"])
    def index():
    query = request.args.get("query", "")
    risultati = cerca_dati(query, DATI_JSON) if query else []
    return render_template_string(template, risultati=risultati, query=query)

    if __name__ == "__main__":
    app.run(debug=True)


    In MsgSetting ther are about 399 key of messages

    "399": { "Background": 0, "Color": "neutral", "Symbol": 0, "Type": 0, "xPos": 0, "yPos": 0 }

    but for example 000 is in different key of key of MsgSettings, parSetting, prog ecc...

    "ParSettings": {
    "Enabled": {
    "Conf": {
    "Flag": {
    },
    "Speed": {
    },
    "Step": {
    "000": true,

    "Prog": {
    "Flag": {
    },
    "Pos": {
    "000": true,
    "Step": {
    "000": true,

    "Timer": {
    "000": true,

    Continue reading...

Compartilhe esta Página