public List<String> getListField(String field) throws JSONException { List<String> rlt = null; if(fields.containsKey(field)) { String value = fields.get(field); JSONArray array = new JSONArray(value); if(array.length() > 0) { rlt = new ArrayList<String>(); for(int i = 0; i < array.length(); i++) { rlt.add(array.getString(i)); } } } return rlt; }
private JSONObject toJSON(AmazonCloudSearchAddRequest document) throws JSONException { JSONObject doc = new JSONObject(); doc.put("type", "add"); doc.put("id", document.id.toLowerCase()); doc.put("version", document.version); doc.put("lang", document.lang); JSONObject fields = new JSONObject(); for(Map.Entry<String, Object> entry : document.fields.entrySet()) { if(entry.getValue() instanceof Collection) { JSONArray array = new JSONArray(); Iterator i = ((Collection)entry.getValue()).iterator(); while(i.hasNext()) { array.put(i.next()); } fields.put(entry.getKey(), array); } else { fields.put(entry.getKey(), entry.getValue()); } } doc.put("fields", fields); return doc; }
private JSONObject decodeParams(String params) { Map<String, String[]> eparams = Utils.decodeQueryString(params); JSONObject paramsObj = new JSONObject(); for (String key : eparams.keySet()) { String[] vals = eparams.get(key); if (vals.length == 1) { try { paramsObj.put(key, Long.parseLong(vals[0])); } catch (NumberFormatException e) { try { paramsObj.put(key, Double.parseDouble(vals[0])); } catch (NumberFormatException e2) { paramsObj.put(key, vals[0]); } } } else{ JSONArray arr = new JSONArray(); for (String s : vals) arr.put(s); paramsObj.put(key, arr); } } return paramsObj; }
private List<String> jsonObjectToListString (JSONArray json) { if (json == null) { return null; } final List<String> list = new ArrayList<>(); for (int i = 0; i < json.length(); i++) { try { list.add(json.getString(i)); } catch (JSONException e) {} } return list; }
public static boolean isJSONValid(String test) { try { new JSONObject(test); } catch (JSONException ex) { try { new JSONArray(test); } catch (JSONException ex1) { return false; } } return true; }
boolean runAllParagraph(String noteId, String hubMsg) { LOG.info("Running paragraph with noteId {}", noteId); try { JSONObject data = new JSONObject(hubMsg); if (data.equals(JSONObject.NULL) || !(data.get("data") instanceof JSONArray)) { LOG.error("Wrong \"data\" format for RUN_NOTEBOOK"); return false; } Client client = Client.getInstance(); if (client == null) { LOG.warn("Base client isn't initialized, returning"); return false; } Message zeppelinMsg = new Message(OP.RUN_PARAGRAPH); JSONArray paragraphs = data.getJSONArray("data"); String principal = data.getJSONObject("meta").getString("owner"); for (int i = 0; i < paragraphs.length(); i++) { if (!(paragraphs.get(i) instanceof JSONObject)) { LOG.warn("Wrong \"paragraph\" format for RUN_NOTEBOOK"); continue; } zeppelinMsg.data = gson.fromJson(paragraphs.getString(i), new TypeToken<Map<String, Object>>(){}.getType()); zeppelinMsg.principal = principal; zeppelinMsg.ticket = TicketContainer.instance.getTicket(principal); client.relayToZeppelin(zeppelinMsg, noteId); LOG.info("\nSending RUN_PARAGRAPH message to Zeppelin "); } } catch (JSONException e) { LOG.error("Failed to parse RUN_NOTEBOOK message from ZeppelinHub ", e); return false; } return true; }
private void notifyComplete( final JSONObject jsonMessage ) throws JSONException { final String jobId = jsonMessage.getString( "jobId" ); final String inputKey = jsonMessage.getJSONObject( "input" ).getString( "key" ); final JSONArray jsonOutputList = jsonMessage.getJSONArray( "outputs" ); for ( int i = 0; i < jsonOutputList.length(); i++ ) { final JSONObject jsonOutput = jsonOutputList.getJSONObject( i ); final String outputKey = jsonOutput.getString( "key" ); final TranscodeEvent completedTranscodeEvent = new TranscodeEvent( jobId, new MovieId( inputKey ), new MovieId( outputKey ) ); this.transcodeEventHandler.onTranscodeComplete( completedTranscodeEvent ); } }
private HNUserItem createUserFromHNAPIResult(String result) { if(result == null || result.isEmpty()) return null; try { HNUserItem useritem = new HNUserItem(); JSONObject profile_jo = new JSONObject(result); useritem.setId(profile_jo.getString("id")); useritem.setCreated(profile_jo.getLong("created")); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); sdf.setTimeZone(TimeZone.getTimeZone("America/Louisville")); useritem.setCreatedHumanReadable(sdf.format(profile_jo.getInt("karma")*1000)); useritem.setKarma(profile_jo.getInt("karma")); if(profile_jo.has("about")) useritem.setAbout(profile_jo.getString("about")); useritem.setDelay(profile_jo.getInt("delay")); if(profile_jo.has("submitted")) { JSONArray ja = profile_jo.getJSONArray("submitted"); HashSet<String> hs = new HashSet<String>(); if (ja != null) { int len = ja.length(); for (int i=0;i<len;i++) { hs.add(ja.get(i).toString()); } useritem.setSubmitted(hs); } } return useritem; } catch (JSONException e) { e.printStackTrace(); return null; } }
/** * Validates a JSON string representing JSON array. * @param text Text to validate * @return True, if text is a valid JSON array. */ private boolean validJSONArray(final String text) { boolean result = true; try { new JSONArray(text); } catch (final JSONException ex) { result = false; } return result; }
private AmazonCloudSearchResult fromJSON(String responseBody) throws JSONException { AmazonCloudSearchResult result = new AmazonCloudSearchResult(); JSONObject root = new JSONObject(responseBody); JSONObject status = root.getJSONObject("status"); if(status != null) { result.rid = status.getString("rid"); result.time = status.getLong("time-ms"); } JSONObject hits = root.getJSONObject("hits"); if(hits != null) { result.found = hits.getInt("found"); result.start = hits.getInt("start"); if(result.found > 0) { JSONArray hitArray = hits.getJSONArray("hit"); if(hitArray != null) { for(int i = 0; i < hitArray.length(); i++) { JSONObject row = hitArray.getJSONObject(i); Hit hit = new Hit(); hit.id = row.getString("id"); JSONObject fields = row.getJSONObject("fields"); String[] names = JSONObject.getNames(fields); for(String name : names) { if(hit.fields == null) { hit.fields = new HashMap<String, String>(); } hit.fields.put(name, fields.getString(name)); } if(result.hits == null) { result.hits = new ArrayList<Hit>(); } result.hits.add(hit); } } } } return result; }
private HNItemItem createItemFromHNAPIResult(String unchecked_result) { if(unchecked_result == null || unchecked_result.isEmpty()) { System.err.println("Error trying to create new item in DB: result string from HN api was null or empty"); return null; } try{ HNItemItem hnii = null; JSONObject new_jo = new JSONObject(unchecked_result); // these are the required fields (as far as we're concerned) // without them, we can't even make sense of what to do with it if(new_jo.has("id") && new_jo.has("by") && new_jo.has("time") && new_jo.has("type")) { //** THESE FIELDS MUST MATCH HNItemItem EXACTLY *** hnii = new HNItemItem(); hnii.setId(new_jo.getLong("id")); hnii.setBy(new_jo.getString("by")); hnii.setTime(new_jo.getLong("time")); hnii.setType(new_jo.getString("type")); if(new_jo.has("dead") && new_jo.getBoolean("dead") == true) hnii.setDead(true); else hnii.setDead(false); if(new_jo.has("deleted") && new_jo.getBoolean("deleted") == true) hnii.setDeleted(true); else hnii.setDeleted(false); if(new_jo.has("parent")) hnii.setParent(new_jo.getLong("parent")); if(new_jo.has("score")) hnii.setScore(new_jo.getLong("score")); if(new_jo.has("kids")) { HashSet<Long> kids_ts = new HashSet<Long>(); JSONArray ja = new_jo.getJSONArray("kids"); if(ja != null && ja.length() > 0) { int x = 0; while(x < ja.length()) { kids_ts.add(ja.getLong(x)); x++; } if(kids_ts.size() == ja.length()) // if the number of items has changed for some reason, just skip bc something has messed up { System.out.println("createHNItemFromHNAPIResult setting kids=" + kids_ts.size()); hnii.setKids(kids_ts); } } else hnii.setKids(null); } if(new_jo.has("url")) hnii.setURL(new_jo.getString("url")); return hnii; } else { System.err.println("Error trying to create new item in DB: missing required id, by, time or type values"); return null; } } catch(JSONException jsone) { System.err.println("Error trying to create new item in DB: result string was not valid JSON."); return null; } }
/** * An add operation specifies either new documents that you want to add to the index or existing documents that you want to update. * An add operation is only applied to an existing document if the version number specified in the operation is greater than the existing document's version number. * * @param documents The documents that need to added or updated * @throws JSONException * @throws AwsCSMalformedRequestException * @throws AwsCSInternalServerException */ public void addDocuments(List<AmazonCloudSearchAddRequest> documents) throws JSONException, AmazonCloudSearchRequestException, AmazonCloudSearchInternalServerException { JSONArray docs = new JSONArray(); for(AmazonCloudSearchAddRequest doc : documents) { docs.put(toJSON(doc)); } updateDocumentRequest(docs.toString()); }
/** * A delete operation specifies existing documents that you want to delete. * A delete operation is only applied to an existing document if the version number specified in the operation is greater than the existing document's version number. * * @param document * @throws AwsCSMalformedRequestException * @throws AwsCSInternalServerException * @throws JSONException */ public void deleteDocuments(List<AmazonCloudSearchDeleteRequest> documents) throws JSONException, AmazonCloudSearchRequestException, AmazonCloudSearchInternalServerException { JSONArray docs = new JSONArray(); for(AmazonCloudSearchDeleteRequest doc : documents) { docs.put(toJSON(doc)); } updateDocumentRequest(docs.toString()); }
/** * An add operation specifies either a new document that you want to add to the index or an existing document that you want to update. * An add operation is only applied to an existing document if the version number specified in the operation is greater than the existing document's version number. * * @param document The document that need to added or updated * @throws AwsCSMalformedRequestException * @throws AwsCSInternalServerException * @throws JSONException */ public void addDocument(AmazonCloudSearchAddRequest document) throws AmazonCloudSearchRequestException, AmazonCloudSearchInternalServerException, JSONException { JSONArray docs = new JSONArray(); docs.put(toJSON(document)); updateDocumentRequest(docs.toString()); }
/** * A delete operation specifies an existing document that you want to delete. * A delete operation is only applied to an existing document if the version number specified in the operation is greater than the existing document's version number. * * @param document * @throws AwsCSMalformedRequestException * @throws AwsCSInternalServerException * @throws JSONException */ public void deleteDocument(AmazonCloudSearchDeleteRequest document) throws AmazonCloudSearchRequestException, AmazonCloudSearchInternalServerException, JSONException { JSONArray docs = new JSONArray(); docs.put(toJSON(document)); updateDocumentRequest(docs.toString()); }