-1

Given the response of a JSON file:

{
    "Response": [
        {
            "ResultCode": "000",
            "ResultMessage": "OK",
            "NRecord": "2",
            "Details": [
                {
                    "nomepacchetto": "Nome Pacchetto",
                    "tipotraffico": "C",
                    "numerominuti": "100.00000",
                    "importocanone": "9.99",
                    "loginultimamodifica": "claudia.mentuccia",
                    "dataorainiziovalidita": "2022-04-28 14:10:01",
                    "loginapprovazione": "",
                    "dataapprovazione": "0000-00-00 00:00:00",
                    "id": "4"
                },
                {
                    "nomepacchetto": "Nome Pacchetto_2",
                    "tipotraffico": "C",
                    "numerominuti": "10.00000",
                    "importocanone": "5.00",
                    "loginultimamodifica": "claudia.mentuccia",
                    "dataorainiziovalidita": "2022-04-28 14:11:29",
                    "loginapprovazione": "",
                    "dataapprovazione": "0000-00-00 00:00:00",
                    "id": "5"
                }
            ]
        }
    ]
}

How can I retrieve all identifying data by id through JavaScript?

function StampaForm() {
   var TblContenuto;
   var TblContenuto = JSON.parse(sessionStorage.wbc_gestionepacchetti800_applicazione);
   var form = "<form action=\"wbc_gestionepacchetti800_3.html\" name='dati' method=\"post\"><div class='tabella_select'><tbody class='body_form'><table class='table table-responsive-sm'>";
   var output = '';
   var output = '<tr>' + '<td class="campi_form col-2 td_body">' + '<p>Nome pacchetto\n</p>' + ' <input type=text class="form-control recordripreso" id="floatingInput" name=nuovoip size=40 value="' + TblContenuto[i].nomepacchetto + '">' + '</td>' + '<td></td>' + '<tr>';
   $("#mod").append(form + output);
}

The variable var TblContenuto = JSON.parse(sessionStorage.wbc_gestionepacchetti800_applicazione); shows all the contents of my JSON file (above), what I would like to do is create a function that extrapolates the data fragment based on the ID, example: id = 5:

{
  "nomepacchetto": "Nome Pacchetto_2",
  "tipotraffico": "C",
  "numerominuti": "10.00000",
  "importocanone": "5.00",
  "loginultimamodifica": "claudia.mentuccia",
  "dataorainiziovalidita": "2022-04-28 14:11:29",
  "loginapprovazione": "",
  "dataapprovazione": "0000-00-00 00:00:00",
   "id": "5"
 }
mentucla
  • 1
  • 2

1 Answers1

-1

You can also do this (avoid to add all element one by one) and gives you the JSON (the code is below the json variable, used for test) :

var json='{ \
    "Response": [ \
        { \
            "ResultCode": "000", \
            "ResultMessage": "OK", \
            "NRecord": "2", \
            "Details": [ \
                { \
                    "nomepacchetto": "Nome Pacchetto", \
                    "tipotraffico": "C", \
                    "numerominuti": "100.00000", \
                    "importocanone": "9.99", \
                    "loginultimamodifica": "claudia.mentuccia", \
                    "dataorainiziovalidita": "2022-04-28 14:10:01", \
                    "loginapprovazione": "", \
                    "dataapprovazione": "0000-00-00 00:00:00", \
                    "id": "4" \
                }, \
                { \
                    "nomepacchetto": "Nome Pacchetto_2", \
                    "tipotraffico": "C", \
                    "numerominuti": "10.00000", \
                    "importocanone": "5.00", \
                    "loginultimamodifica": "claudia.mentuccia", \
                    "dataorainiziovalidita": "2022-04-28 14:11:29", \
                    "loginapprovazione": "", \
                    "dataapprovazione": "0000-00-00 00:00:00", \
                    "id": "5" \
                } \
            ] \
        } \
    ] \
}';
test=JSON.parse(json);
response=test.Response;
response[0].Details.forEach(function(element ) {
    if (element["id"]=="5") { //Change this value to get the id you want
    $("#result").html(JSON.stringify(element)); //Remove function to have the variable (element)
    $.each(element, function(key, value) {
      $("#calc").append("Key :",key,"  Value:",value,"<br/>");
    });
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="result">
</div>
<div id="calc">
</div>
JoelCrypto
  • 462
  • 2
  • 12
  • Thank you so much for answering me, that's so kind of you, the script works! is it possible instead to write all the data inside the file by hand and create a dynamic thing? – mentucla May 03 '22 at 11:47
  • Sure, as long as your variable is JSON-like, you can get it from any sources (file, website requests, php...). – JoelCrypto May 03 '22 at 12:00
  • Hi @JoelCrypto , I can't transform that variable of yours 'json' dynamically, i.e. set the declared variable as you put it, everything works correctly with my array variable `var TblContenuto = JSON.parse(sessionStorage.wbc_gestionepacchetti800_applicazione);` it doesn't work, could you help me? – mentucla May 05 '22 at 07:56
  • Does your variable looks the same way as the array above? Where did you put this code? – JoelCrypto May 05 '22 at 10:15
  • You dont have anymore response or details so remove this part. – JoelCrypto May 05 '22 at 11:43