Home > Enterprise >  can't get the Element of JSON Object
can't get the Element of JSON Object

Time:04-01

I have a jsonobject that contains some json objects

  "paths": {
    "/api/{version}/RAW/getrawdata": {
      "get": {
        "tags": [
          "RAw"
        ],
        "summary": "/getrawdata",
        "parameters": [
          {
            "name": "token",
            "in": "header",
            "description": "gettoken",
            "schema": {
              "type": "string"
            },
    "/api/{version}/filtered/getfinaldatadata": {
    "get": {
        "tags": [
          "filtered"
        ],
        "summary": "/getfinaldatadata",
Now I want to get the elements summary and tags and want to return the values. Its easy to get the value of summary but tags has [] so I don't know how to take the value e.g RAW . I was trying something like this

package beans;

import org.apache.camel.Exchange;
import org.apache.camel.Header;
public class URIpattern {
    @SuppressWarnings("deprecation")
    public String URI(JSONObject json,
            @Header (Exchange.SLIP_ENDPOINT) String previous) {
        if(previous!=null){
             return null;


  JSONObject paths= json.getJSONObject("paths");
  JSONObject summary = operation.getString("summary");
    JSONObject tags = operation.getJSONObject("tags");
      return tags   summary;

but it is not doing any thing. Can someone please guide me since I am new to java. Any help would be appreciated. Thanks in Advance

CodePudding user response:

Well I guess you are using this Object and inside the defined class you have the function getJSONArray after that just use the new ArrayObject and pull the data what you need out of it.

CodePudding user response:

In JSON values are present in [] are treated as arrays. So if you need to extract the tags, you will need to get them in a array like,

 String json = "{\"tags\":[\"1\",\"2\",\"3\"]}";
 JSONObject object = new JSONObject(json);
 JSONArray array = object.getJSONArray("tags");
 System.out.println(array);

P.S. I could not find the json posted in question to be valid, hence I have used some exmaple to explain the JSONArray.

  • Related