Github user zenfenan commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2760#discussion_r196073711
  
    --- Diff: 
nifi-nar-bundles/nifi-elasticsearch-bundle/nifi-elasticsearch-processors/src/main/java/org/apache/nifi/processors/elasticsearch/PutElasticsearchHttp.java
 ---
    @@ -288,42 +290,23 @@ public void onTrigger(final ProcessContext context, 
final ProcessSession session
                 session.read(file, in -> {
                     json.append(IOUtils.toString(in, charset).replace("\r\n", 
" ").replace('\n', ' ').replace('\r', ' '));
                 });
    -            if (indexOp.equalsIgnoreCase("index")) {
    -                sb.append("{\"index\": { \"_index\": \"");
    -                sb.append(index);
    -                sb.append("\", \"_type\": \"");
    -                sb.append(docType);
    -                sb.append("\"");
    -                if (!StringUtils.isEmpty(id)) {
    -                    sb.append(", \"_id\": \"");
    -                    sb.append(id);
    -                    sb.append("\"");
    -                }
    -                sb.append("}}\n");
    -                sb.append(json);
    -                sb.append("\n");
    -            } else if (indexOp.equalsIgnoreCase("upsert") || 
indexOp.equalsIgnoreCase("update")) {
    -                sb.append("{\"update\": { \"_index\": \"");
    -                sb.append(index);
    -                sb.append("\", \"_type\": \"");
    -                sb.append(docType);
    -                sb.append("\", \"_id\": \"");
    -                sb.append(id);
    -                sb.append("\" }\n");
    -                sb.append("{\"doc\": ");
    -                sb.append(json);
    -                sb.append(", \"doc_as_upsert\": ");
    -                sb.append(indexOp.equalsIgnoreCase("upsert"));
    -                sb.append(" }\n");
    -            } else if (indexOp.equalsIgnoreCase("delete")) {
    -                sb.append("{\"delete\": { \"_index\": \"");
    -                sb.append(index);
    -                sb.append("\", \"_type\": \"");
    -                sb.append(docType);
    -                sb.append("\", \"_id\": \"");
    -                sb.append(id);
    -                sb.append("\" }\n");
    +
    +            String jsonString = json.toString();
    +
    +            // Ensure the JSON body is well-formed
    +            try {
    --- End diff --
    
    @ottobackwards @mattyb149 As part of NIFI-5261 and NIFI-5271 introduced a 
JSON validator. I'm taking this as an opportunity to share it with you guys and 
whoever takes a look at this comment. It could be of use here and in your 
future contributions.


---

Reply via email to