0

My question is, how can you read a specefic form of json data using javascript, for example if i had this one, it's seems to me a bet difficult, so can you help me out.

{
  "jQRReponse": [
    [
      {
        "sujet": "RHONE ALPES",
        "verbe": "est_le_nom_de_la_region",
        "complement": {
          "sujet": "82",
          "verbe": "est_la_region_du_dept",
          "complement": {
            "sujet": "01",
            "verbe": "est_le_numero_du_dept",
            "complement": {
              "sujet": "Ain",
              "verbe": "contient_les_resultats_de_depAnn",
              "complement": {
                "sujet": "Ain2014",
                "verbe": "Pop_results_Ens_Total",
                "complement": "626794"
              }
            }
          }
        }
      }
    ],
    [
      {
        "sujet": "RHONE-ALPES",
        "verbe": "est_le_nom_de_la_region",
        "complement": {
          "sujet": "82",
          "verbe": "est_la_region_du_dept",
          "complement": {
            "sujet": "01",
            "verbe": "est_le_numero_du_dept",
            "complement": {
              "sujet": "Ain",
              "verbe": "contient_les_resultats_de_depAnn",
              "complement": {
                "sujet": "Ain2014",
                "verbe": "Pop_results_Ens_Total",
                "complement": "626794"
              }
            }
          }
        }
      }
    ]
  ]
}

Say if I had this form:

data : [{
toto:5,
mama:10
},
{
toto:99,
mama:10
},
{
toto:88,
mama:10
}]

I'm gonna read the toto value at the index i like this : data[i].toto.

So how can I do it for the first one.

Thank you

5
  • which key value you want to read? Commented Jul 18, 2016 at 7:12
  • Acttually I want to be able to read all values that I can use them later Commented Jul 18, 2016 at 7:13
  • @YahyaAlami first you parse the data in the javascript to json object, you can visualise your data here jsoneditoronline.org , then using obj.jQRReponse[1].sujet etc , just a tip Commented Jul 18, 2016 at 7:16
  • well thank you @ARUN I will try this Commented Jul 18, 2016 at 7:19
  • Possible duplicate of How to access nested JSON data Commented Jul 18, 2016 at 7:36

2 Answers 2

1

You could iterate over all data and get the ressult.

The access is in the form like

data.jQRReponse[0][0].complement.complement.sujet
// returns "01"

function read(o) {
    Object.keys(o).forEach(function (k) {
        if (o[k] !== null && typeof o[k] === 'object') {
            console.log(k + ' -->');
            read(o[k]);
            return;
        }
        console.log(k + ': ' + o[k]);
    });
}

var data = { "jQRReponse": [[{ "sujet": "RHONE ALPES", "verbe": "est_le_nom_de_la_region", "complement": { "sujet": "82", "verbe": "est_la_region_du_dept", "complement": { "sujet": "01", "verbe": "est_le_numero_du_dept", "complement": { "sujet": "Ain", "verbe": "contient_les_resultats_de_depAnn", "complement": { "sujet": "Ain2014", "verbe": "Pop_results_Ens_Total", "complement": "626794" } } } } }], [{ "sujet": "RHONE-ALPES", "verbe": "est_le_nom_de_la_region", "complement": { "sujet": "82", "verbe": "est_la_region_du_dept", "complement": { "sujet": "01", "verbe": "est_le_numero_du_dept", "complement": { "sujet": "Ain", "verbe": "contient_les_resultats_de_depAnn", "complement": { "sujet": "Ain2014", "verbe": "Pop_results_Ens_Total", "complement": "626794" } } } } }]] };

read(data, []);

Sign up to request clarification or add additional context in comments.

Comments

1

You can access the specific part of this nested json object if you know the element, depth and the key.

Say if your element is 0, depth is 2 and the key is verbe, then you can do like this:

nestedObj[0].complement.complement.complement.verbe

If your requirement is to traverse the whole object and find something you need, you can use a recursive function for each element.

function traverse_it(obj){

    for(var prop in obj){

        console.log(obj[prop]);            //Or whatever you want to do with this level

        if(typeof obj[prop]=='object'){
           traverse_it(obj[prop[i]]);      //Function goes to the next level here

        }
    }
}

traverse_it(nestedObj);

Here is lot of insight

3 Comments

no that's incorrect it must be like this actually nestedObj[i][0], but I thank you tho for your answer
My answer considered only the obj part. Not the array which contained multitudes of it.
oh ok ! In this case I have no problems with it, thank's

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.