0

I want to get JSON with curl command, so with below command I am getting output:

curl -GET http://localhost:9200/oldindex/_mapping?pretty
{
  "gl-events_1" : {
    "mappings" : {
      "message" : {
        "dynamic" : "false",
        "dynamic_templates" : [
          {
            "fields" : {
              "path_match" : "fields.*",
              "mapping" : {
                "doc_values" : true,
                "index" : true,
                "type" : "keyword"
              }
            }
          }
        ],
        "properties" : {
          "alert" : {
            "type" : "boolean"
          },
          "event_definition_id" : {
            "type" : "keyword"
          },
          "event_definition_type" : {
            "type" : "keyword"
          },
          "fields" : {
            "type" : "object",
            "dynamic" : "true"
          },
          "id" : {
            "type" : "keyword"
          },
          "key" : {
            "type" : "keyword"
          },
          "key_tuple" : {
            "type" : "keyword"
          },
          "message" : {
            "type" : "text",
            "norms" : false,
            "fields" : {
              "keyword" : {
                "type" : "keyword"
              }
            },
            "analyzer" : "standard"
          },
          "origin_context" : {
            "type" : "keyword"
          },
          "priority" : {
            "type" : "long"
          },
          "source" : {
            "type" : "keyword"
          },
          "source_streams" : {
            "type" : "keyword"
          },
          "streams" : {
            "type" : "keyword"
          },
          "timerange_end" : {
            "type" : "date",
            "format" : "yyyy-MM-dd HH:mm:ss.SSS"
          },
          "timerange_start" : {
            "type" : "date",
            "format" : "yyyy-MM-dd HH:mm:ss.SSS"
          },
          "timestamp" : {
            "type" : "date",
            "format" : "yyyy-MM-dd HH:mm:ss.SSS"
          },
          "timestamp_processing" : {
            "type" : "date",
            "format" : "yyyy-MM-dd HH:mm:ss.SSS"
          },
          "triggered_jobs" : {
            "type" : "keyword"
          }
        }
      }
    }
  }
}

Now i want to store this output as json file so I copied it in file and gave extension as .json

But when i try to put with curl I am getting below error

curl -X PUT http://localhost:9200/new_good -H 'Content-Type: application/json' -d  sampl.json
{"error":{"root_cause":[{"type":"not_x_content_exception","reason":"Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes"}],"type":"not_x_content_exception","reason":"Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes"},"status":500}

But when i run below command with same json format directly it works,

curl -X PUT \
http://localhost:9200/new_good \
-H 'Content-Type: application/json' \
-d '{"mappings" : {
      "message" : {
        "dynamic_templates" : [
          {
            "internal_fields" : {
              "match" : "gl2_*",
              "match_mapping_type" : "string",
              "mapping" : {
                "type" : "keyword"
              }
            }
          },
          {
            "store_generic" : {
              "match_mapping_type" : "string",
              "mapping" : {
                "type" : "keyword"
              }
            }
          }
        ],
        "properties" : {
          "LoggerName" : {
            "type" : "keyword"
          },
          "MessageParam0" : {
            "type" : "keyword"
          },
          "MessageParam1" : {
            "type" : "long"
          },
          "MessageParam2" : {
            "type" : "keyword"
          },
          "MessageParam3" : {
            "type" : "keyword"
          },
          "MessageParam4" : {
            "type" : "keyword"
          },
          "MessageParam5" : {
            "type" : "keyword"
          },
          "MessageParam6" : {
            "type" : "keyword"
          },
          "MessageParam7" : {
            "type" : "keyword"
          },
          "MessageParam8" : {
            "type" : "keyword"
          },
          "Severity" : {
            "type" : "keyword"
          },
          "SourceClassName" : {
            "type" : "keyword"
          },
          "SourceMethodName" : {
            "type" : "keyword"
          },
          "SourceSimpleClassName" : {
            "type" : "keyword"
          },
          "StackTrace" : {
            "type" : "keyword"
          },
          "Thread" : {
            "type" : "keyword"
          },
          "Time" : {
            "type" : "keyword"
          },
          "facility" : {
            "type" : "keyword"
          },
          "full_message" : {
            "type" : "text",
            "analyzer" : "standard"
          },
          "gl2_accounted_message_size" : {
            "type" : "long"
          },
          "gl2_message_id" : {
            "type" : "keyword"
          },
          "gl2_processing_timestamp" : {
            "type" : "date",
            "format" : "yyyy-MM-dd HH:mm:ss.SSS"
          },
          "gl2_receive_timestamp" : {
            "type" : "date",
            "format" : "yyyy-MM-dd HH:mm:ss.SSS"
          },
          "gl2_remote_ip" : {
            "type" : "keyword"
          },
          "gl2_remote_port" : {
            "type" : "long"
          },
          "gl2_source_input" : {
            "type" : "keyword"
          },
          "gl2_source_node" : {
            "type" : "keyword"
          },
          "level" : {
            "type" : "long"
          },
          "message" : {
            "type" : "text",
            "analyzer" : "standard"
          },
          "source" : {
            "type" : "text",
            "analyzer" : "analyzer_keyword",
            "fielddata" : true
          },
          "streams" : {
            "type" : "keyword"
          },
          "timestamp" : {
            "type" : "date",
            "format" : "yyyy-MM-dd HH:mm:ss.SSS"
          }
        }
      }
    }
  }
}'

What I want is store curl GET command output as valid json which I can use in curl PUT,

curl get > some.json
curl put -d some.json 

I am new to this and i tried several options with jq as well but that also didn't workd for me.

Please guide me here.

Regards SAM

1 Answer 1

0

To save the JSON from the curl command, you can redirect the output to a file in your case. Or use:

curl ... -o file.json

To send it with curl, you can run your command with

curl ... -d @file.json

From man curl for -d --data <data>:

If you start the data with the letter @, the rest should be a file name to read the data from, or - if you want curl to read the data from stdin.

Also piping the json to the stdin of the curl command would work, with -d -.

4
  • Hello Thanasisp, I tried "curl -X PUT localhost:9200/holly_good -H 'Content-Type: application/json' -d @new.json " but it gave below error {"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"analyzer [analyzer_keyword] not found for field [source]"}],"type":"mapper_parsing_exception","reason":"Failed to parse mapping [message]: analyzer [analyzer_keyword] not found for field [source]","caused_by":{"type":"mapper_parsing_exception","reason":"analyzer [analyzer_keyword] not found for field [source]"}},"status":400} can you guide what I am doing wrong
    – Samurai
    Commented Jun 1, 2022 at 7:41
  • Hello @Samurai. This error (400) is different than the 500 (internal server error) you got before. It means "bad request". It seems that your server is reading the JSON data but it doesn't find an expected field ("analyzer" for "source"). It is related to your server/data/processing.
    – thanasisp
    Commented Jun 1, 2022 at 7:49
  • Thank you Thanasisp, I will check and confirm
    – Samurai
    Commented Jun 1, 2022 at 8:21
  • @Samurai If this solves your issue, please consider accepting the answer. Accepting an answer marks the issue as resolved.
    – Kusalananda
    Commented Jul 28, 2022 at 15:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .