How do I extract the "extract" attribute from JSON?

17.2k views Asked by At

I am trying read JSON data from this link.

I am able to read the entire data inside pages attribute using :

JSONObject data=(JSONObject)new JSONTokener(IOUtils.toString(new URL("https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles=Russell%27s_paradox"))).nextValue();
String pageName=data.getJSONObject("query").getString("pages");
System.out.println(pageName);

I want to get the data from "extract" attribute next from the above mentioned link, which I am unable to. I am new with this and I can't find any resources to learn this.

I tried the following code but I am getting a JSONException.

JSONArray arr=data.getJSONArray("pages");
for(int i=0;i<arr.length();i++){
String def=arr.getJSONObject(i).getString("extract");
System.out.println(def);
}

Help.

2

There are 2 answers

5
janos On BEST ANSWER

The structure of the JSON you're trying to parse is this:

{
  "batchcomplete": "",
  "query": {
    "normalized": [
      {
        "from": "Russell's_paradox",
        "to": "Russell's paradox"
      }
    ],
    "pages": {
      "46095": {
        "pageid": 46095,
        "ns": 0,
        "title": "Russell's paradox",
        "extract": "..."
      }

That is, pages inside query is an object, not an array. And then, extract is within another nested object, with key 46095. You can get to the extract field like this:

JSONObject pages = data.getJSONObject("query").getJSONObject("pages");
for (String key : pages.keySet()) {
    String def = pages.getJSONObject(key).getString("extract");
    System.out.println(def);
}
0
Bahadur Singh On

Using node JS you can get by its attribute name and also navigate to inner nodes by for loop or recursion depending the depth of JSON object. Here is simple example using for loop for 2 level attribute / data of JSON:

const jsonStudent = '{"name":"Mond S","fields":{"Age":12,"Class":"Grad 8","School":"In Town"}}';
 get_JSON_Data ();

function println (msg)
{
  if (DEBUG) {
    var jsonDate = (new Date()).toJSON();
    console.log( jsonDate + ' myParser ' + msg);
  }
}

function get_JSON_Data ()
{
  DEBUG = true;
  var obj = JSON.parse(jsonStudent); // This obj is json object made from string 
  for (var k in obj)
  {
    println ("Got Key " + k + " = " + obj [k]);
    if (typeof (obj [k]) == 'object')
    {
      var felds = obj [k];
      for (var f in felds)
        println ( "    Got fields attr " + f + "   = " + felds [f] + ", direct from obj " + obj[k][f]);
    }  
  }
}

==== output will be

2020-12-26T13:06:47.645Z myXMLParser Got Key name = Mond S
2020-12-26T13:06:47.649Z myXMLParser Got Key fields = [object Object]
2020-12-26T13:06:47.649Z myXMLParser     Got fields attr Age   = 12 ,direct from obj 22
2020-12-26T13:06:47.649Z myXMLParser     Got fields attr Class   = Grad 8 ,direct from obj Grad 8
2020-12-26T13:06:47.649Z myXMLParser     Got fields attr School   = In Town ,direct from obj In Town