Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session
SOLVED

Extract keys from Json Node

Avatar

Community Advisor

Hi,

I do have a com.fasterxml.jackson.databind.JsonNode

 format like this : 

{ "jcr:primaryType": "nt:unstructured", "sling:resourceType": "foundation/components/parsys", "rwdcarousel": { "jcr:primaryType": "nt:unstructured", "jcr:createdBy": "admin", "contentFinderImage": "/etc/designs/webcms-ngtv/globallibs/img/rwdCarouselModuleIcon.jpg", "rwdcarouselType": "singleCarousel", "jcr:lastModifiedBy": "admin" } }

Out of this, I need to fetch keys which contains values inside { }. Like, in this case, rwdcarousel.

1 Accepted Solution

Avatar

Correct answer by
Level 10

1. Put the complete json in JSONObject x = new JSONObject(your_json).

2. Execute x.getJSONObject("social");

Take a look @ http://crunchify.com/java-how-to-parse-jsonobject-and-jsonarrays/

View solution in original post

2 Replies

Avatar

Correct answer by
Level 10

1. Put the complete json in JSONObject x = new JSONObject(your_json).

2. Execute x.getJSONObject("social");

Take a look @ http://crunchify.com/java-how-to-parse-jsonobject-and-jsonarrays/

Avatar

Administrator

Hi

Please have a look at these post to get answer to your question:

1. JavaScript

// Link:- http://stackoverflow.com/questions/18080543/how-to-extract-keys-from-key-value-json-object

Let say, your Json is:

{"16": {
    "events": {
      "burstevents": {
          "1369353600000.0": "maj", "1371600000000.0": "maj", "1373414400000.0": "maj", "1373500800000.0": "maj", "1373673600000.0": "maj"
        }, 
      "sentevents": {
          "1370736000000.0": "pos", "1370822400000.0": "pos", "1370908800000.0": "pos"
        }
     }
  }
}

You can iterate over the keys using the in keyword.

var json = $.parseJSON(data);
var keys = array();
for(var key in json[16].events.burstevents)
{
    keys.push(key);
}
You can do it with jQuery

var json = $.parseJSON(data);
var keys = $.map(json[16].events.burstevents,function(v,k) { return k; });
You can use JavaScript Object

var json = $.parseJSON(data);
var keys = Object.keys(json[16].events.burstevents);

2. Java

//Link:- http://stackoverflow.com/questions/4407532/parse-json-object-with-string-and-value-only

 String s = "{menu:{\"1\":\"sql\", \"2\":\"android\", \"3\":\"mvc\"}}";
    JSONObject jObject  = new JSONObject(s);
    JSONObject  menu = jObject.getJSONObject("menu");

    Map<String,String> map = new HashMap<String,String>();
    Iterator iter = menu.keys();
    while(iter.hasNext()){
        String key = (String)iter.next();
        String value = menu.getString(key);
        map.put(key,value);
    }

Thanks and Regards

Kautuk Sahni



Kautuk Sahni