Example usage for org.json.simple JSONObject get

List of usage examples for org.json.simple JSONObject get

Introduction

In this page you can find the example usage for org.json.simple JSONObject get.

Prototype

V get(Object key);

Source Link

Document

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

Usage

From source file:com.twosigma.beaker.core.rest.UtilRest.java

/**
 * This function returns a Boolean setting as string. If the setting is null in the preference,
 * it will use the setting in the config, otherwise, what is set in preference is used.
 * @param settingsListName//from  w w w .ja v  a 2 s. c  o  m
 * @param configs
 * @param prefs
 * @return
 */
private static String mergeBooleanSetting(String settingName, JSONObject configs, JSONObject prefs) {
    Boolean sc = (Boolean) configs.get(settingName);
    Boolean sp = (Boolean) prefs.get(settingName);
    Boolean s = (sp != null) ? sp : sc;
    String setting = null;
    if (s != null) {
        if (s.equals(Boolean.TRUE)) {
            setting = "true";
        } else if (s.equals(Boolean.FALSE)) {
            setting = "false";
        }
    }
    return setting;
}

From source file:org.apache.metron.elasticsearch.integration.ElasticsearchSearchIntegrationTest.java

protected static void loadTestData() throws ParseException, IOException {
    // add bro template
    JSONObject broTemplate = JSONUtils.INSTANCE.load(new File(broTemplatePath), JSONObject.class);
    addTestFieldMappings(broTemplate, "bro_doc");
    String broTemplateJson = JSONUtils.INSTANCE.toJSON(broTemplate, true);
    HttpEntity broEntity = new NStringEntity(broTemplateJson, ContentType.APPLICATION_JSON);
    Response response = lowLevelClient.performRequest("PUT", "/_template/bro_template", Collections.emptyMap(),
            broEntity);//w  w w  . j ava  2 s  .c  o m
    assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
    // add snort template
    JSONObject snortTemplate = JSONUtils.INSTANCE.load(new File(snortTemplatePath), JSONObject.class);
    addTestFieldMappings(snortTemplate, "snort_doc");
    String snortTemplateJson = JSONUtils.INSTANCE.toJSON(snortTemplate, true);
    HttpEntity snortEntity = new NStringEntity(snortTemplateJson, ContentType.APPLICATION_JSON);
    response = lowLevelClient.performRequest("PUT", "/_template/snort_template", Collections.emptyMap(),
            snortEntity);
    assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
    // create bro index
    response = lowLevelClient.performRequest("PUT", BRO_INDEX);
    assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
    // create snort index
    response = lowLevelClient.performRequest("PUT", SNORT_INDEX);
    assertThat(response.getStatusLine().getStatusCode(), equalTo(200));

    JSONArray broRecords = (JSONArray) new JSONParser().parse(broData);

    BulkRequest bulkRequest = new BulkRequest();
    for (Object o : broRecords) {
        JSONObject json = (JSONObject) o;
        IndexRequest indexRequest = new IndexRequest(BRO_INDEX, "bro_doc", (String) json.get("guid"));
        indexRequest.source(json);
        indexRequest.timestamp(json.get("timestamp").toString());
        bulkRequest.add(indexRequest);
    }
    bulkRequest.setRefreshPolicy(RefreshPolicy.WAIT_UNTIL);
    BulkResponse bulkResponse = highLevelClient.bulk(bulkRequest);
    assertFalse(bulkResponse.hasFailures());
    assertThat(bulkResponse.status().getStatus(), equalTo(200));

    JSONArray snortRecords = (JSONArray) new JSONParser().parse(snortData);

    bulkRequest = new BulkRequest();
    for (Object o : snortRecords) {
        JSONObject json = (JSONObject) o;
        IndexRequest indexRequest = new IndexRequest(SNORT_INDEX, "snort_doc", (String) json.get("guid"));
        indexRequest.source(json);
        indexRequest.timestamp(json.get("timestamp").toString());
        bulkRequest.add(indexRequest);
    }
    bulkRequest.setRefreshPolicy(RefreshPolicy.WAIT_UNTIL);
    bulkResponse = highLevelClient.bulk(bulkRequest);
    assertFalse(bulkResponse.hasFailures());
    assertThat(bulkResponse.status().getStatus(), equalTo(200));
}

From source file:com.memetix.mst.MicrosoftTranslatorAPI.java

private static String[] jsonToStringArr(final String inputString, final String propertyName) throws Exception {
    final JSONArray jsonArr = (JSONArray) JSONValue.parse(inputString);
    String[] values = new String[jsonArr.size()];

    int i = 0;/*w w  w  .j  a va  2 s . com*/
    for (Object obj : jsonArr) {
        if (propertyName != null && propertyName.length() != 0) {
            final JSONObject json = (JSONObject) obj;
            if (json.containsKey(propertyName)) {
                values[i] = json.get(propertyName).toString();
            }
        } else {
            values[i] = obj.toString();
        }
        i++;
    }
    return values;
}

From source file:gov.nasa.jpl.xdata.nba.impoexpo.parse.ParseUtil.java

public static AvailableVideo parseAvailableVideo(JSONObject jsonObject) {
    JSONArray resultSets = (JSONArray) jsonObject.get("resultSets");
    JSONObject availableVideoObject = (JSONObject) resultSets.get(10);
    JSONArray rowSet = (JSONArray) availableVideoObject.get("rowSet");
    JSONArray row = (JSONArray) rowSet.get(0);
    return AvailableVideo.newBuilder().setVideoAvailableFlag((Long) row.get(0)).build();
}

From source file:com.apigee.edge.config.utils.ConsolidatedConfigReader.java

/**
 * Example Hierarchy//  w  w  w . j  a v a  2  s.  c  o m
 * orgConfig.apiProducts
 *
 * Returns List of
 * [ {apiProduct1}, {apiProduct2}, {apiProduct3} ]
 */
public static List getOrgConfig(File configFile, String scope, String resource)
        throws ParseException, IOException {

    Logger logger = LoggerFactory.getLogger(ConfigReader.class);

    JSONParser parser = new JSONParser();
    ArrayList out = null;
    try {
        BufferedReader bufferedReader = new BufferedReader(new java.io.FileReader(configFile));

        JSONObject edgeConf = (JSONObject) parser.parse(bufferedReader);
        if (edgeConf == null)
            return null;

        JSONObject scopeConf = (JSONObject) edgeConf.get(scope);
        if (scopeConf == null)
            return null;

        JSONArray configs = (JSONArray) scopeConf.get(resource);
        if (configs == null)
            return null;

        out = new ArrayList();
        for (Object config : configs) {
            out.add(((JSONObject) config).toJSONString());
        }
    } catch (IOException ie) {
        logger.info(ie.getMessage());
        throw ie;
    } catch (ParseException pe) {
        logger.info(pe.getMessage());
        throw pe;
    }

    return out;
}

From source file:gov.nasa.jpl.xdata.nba.impoexpo.parse.ParseUtil.java

public static GameInfo parseInfo(JSONObject jsonObject) {
    JSONArray resultSets = (JSONArray) jsonObject.get("resultSets");
    JSONObject infoObject = (JSONObject) resultSets.get(8);
    JSONArray rowSet = (JSONArray) infoObject.get("rowSet");
    JSONArray row = (JSONArray) rowSet.get(0);
    String gameDate = (String) row.get(0);
    long attendance = (Long) row.get(1);
    String gameTime = (String) row.get(2);
    return GameInfo.newBuilder().setGameDate(gameDate).setAttendance(attendance).setGameTime(gameTime).build();
}

From source file:com.github.lgi2p.obirs.utils.JSONConverter.java

public static RefinedObirsQuery parseRefinedObirsQuery(SM_Engine engine, String jsonQuery)
        throws ParseException, IOException, Exception {

    logger.info("parsing json refined query");
    logger.info("query: " + jsonQuery);
    jsonQuery = jsonQuery.replace("\n", "");
    jsonQuery = jsonQuery.replace("\r", "");

    JSONParser parser = new JSONParser();
    Object obj = parser.parse(jsonQuery);
    JSONObject jsonObject = (JSONObject) obj;

    JSONObject _query = (JSONObject) jsonObject.get("query");
    StringWriter jsQuery = new StringWriter();
    _query.writeJSONString(jsQuery);//from  w  ww.j  a va 2  s . c  om
    ObirsQuery obirsQuery = parseObirsJSONQuery(engine, jsQuery.toString());

    List<URI> selectedItemURIs = toURIs((List<String>) jsonObject.get("selectedItemURIs"));
    List<URI> rejectedItemURIs = toURIs((List<String>) jsonObject.get("rejectedItemURIs"));

    return new RefinedObirsQuery(obirsQuery, selectedItemURIs, rejectedItemURIs);
}

From source file:com.apigee.edge.config.utils.ConsolidatedConfigReader.java

/**
 * Example Hierarchy//  w  w  w  .  ja  v  a 2s . c  o  m
 * envConfig.cache.<env>.caches
 * 
 * Returns List of
 * [ {cache1}, {cache2}, {cache3} ]
 */
public static List getEnvConfig(String env, File configFile, String scope, String resource)
        throws ParseException, IOException {

    Logger logger = LoggerFactory.getLogger(ConfigReader.class);

    JSONParser parser = new JSONParser();
    ArrayList out = null;
    try {
        BufferedReader bufferedReader = new BufferedReader(new java.io.FileReader(configFile));

        JSONObject edgeConf = (JSONObject) parser.parse(bufferedReader);
        if (edgeConf == null)
            return null;

        JSONObject scopeConf = (JSONObject) edgeConf.get(scope);
        if (scopeConf == null)
            return null;

        JSONObject envConf = (JSONObject) scopeConf.get(env);
        if (envConf == null)
            return null;

        JSONArray configs = (JSONArray) envConf.get(resource);
        if (configs == null)
            return null;

        out = new ArrayList();
        for (Object config : configs) {
            out.add(((JSONObject) config).toJSONString());
        }
    } catch (IOException ie) {
        logger.info(ie.getMessage());
        throw ie;
    } catch (ParseException pe) {
        logger.info(pe.getMessage());
        throw pe;
    }

    return out;
}

From source file:com.apigee.edge.config.utils.ConsolidatedConfigReader.java

/**
 * API Config//from www.jav a2 s .c  o  m
 * [ {apiProduct1}, {apiProduct2}, {apiProduct3} ]
 */
public static List getAPIConfig(File configFile, String api, String resource)
        throws ParseException, IOException {

    Logger logger = LoggerFactory.getLogger(ConfigReader.class);

    JSONParser parser = new JSONParser();
    ArrayList out = null;
    try {
        BufferedReader bufferedReader = new BufferedReader(new java.io.FileReader(configFile));

        JSONObject edgeConf = (JSONObject) parser.parse(bufferedReader);
        if (edgeConf == null)
            return null;

        JSONObject scopeConf = (JSONObject) edgeConf.get("apiConfig");
        if (scopeConf == null)
            return null;

        JSONObject scopedConfigs = (JSONObject) scopeConf.get(api);
        if (scopedConfigs == null)
            return null;

        JSONArray resourceConfigs = (JSONArray) scopedConfigs.get(resource);
        if (resourceConfigs == null)
            return null;

        out = new ArrayList();
        for (Object config : resourceConfigs) {
            out.add(((JSONObject) config).toJSONString());
        }
    } catch (IOException ie) {
        logger.info(ie.getMessage());
        throw ie;
    } catch (ParseException pe) {
        logger.info(pe.getMessage());
        throw pe;
    }

    return out;
}

From source file:com.worldline.easycukes.commons.helpers.JSONHelper.java

/**
 * Returns the value of a given property from a particular
 * {@link JSONObject}/*from  w  ww  .j a v a2  s .  com*/
 *
 * @param jsonObject the {@link JSONObject} to be used for extracting values
 * @param property   the property you'd like to get from the specified
 *                   {@link JSONObject}
 * @return the value of the specified property if it's found, or null if it
 * cannot be found
 */
public static String getValue(@NonNull final JSONObject jsonObject, @NonNull final String property) {
    if (jsonObject != null)
        if (jsonObject.get(property) != null)
            return jsonObject.get(property).toString();
    final int index = property.indexOf(".");
    if (index > 0) {
        final Object object = getProperty(jsonObject, property.substring(0, index));
        if (object != null) {
            if (object instanceof JSONObject)
                return getValue((JSONObject) object, property.substring(index + 1));
            if (object instanceof JSONArray) {
                final JSONArray jsonArray = (JSONArray) object;
                if (jsonArray.size() > 0)
                    return getValue((JSONObject) jsonArray.get(0), property.substring(index + 1));
            }
        }
    }
    return null;
}