Script to read json in node.js and generate an elasticsearch bulk json file

Make sure node.js is installed before using this solution.

  • Configure the below javascript file.
  • You can use id as sequene no. or else you can use the id from the document please configure stream.write in for loop accordingly.

json_to_elasticsearch_compatible_json.js

//Reads json file and generates elasticsearch bulk api compatible json document

var inputFile = "/Users/saurabh/Downloads/amazon.json";

var outputFile = "/Users/saurabh/Downloads/amazon_elasticsearch_compatible.json";

var index = "products";

var type= "post";

var fs = require('fs');

var jsonContent = JSON.parse(fs.readFileSync(inputFile, 'utf8'));

var stream = fs.createWriteStream(outputFile);

stream.once('open', function(fd) {

  for(var object in jsonContent)
  {
      //For _id as sequence no.
      stream.write("{ \"index\" : { \"_index\" : \""+index+"\", \"_type\" : \""+type+"\", \"_id\" : \""+object+"\" } }\n")

      //For _id as document id
      //stream.write("{ \"index\" : { \"_index\" : \""+index+"\", \"_type\" : \""+type+"\", \"_id\" : \""+jsonContent[object].id+"\" } }\n")

    stream.write(JSON.stringify(jsonContent[object])+"\n")
  }

  stream.end();
});

Input json:

  {
    "id":"b000jz4hqo",
    "title":"clickart 950 000 - premier image pack (dvd-rom)",
    "description":"",
    "manufacturer":"broderbund",
    "price":29.99
  },
  {
    "id":"b0006zf55o",
    "title":"ca international - arcserve lap/desktop oem 30pk",
    "description":"oem arcserve backup v11.1 win 30u for laptops and desktops",
    "manufacturer":"computer associates",
    "price":19.99
  }
]

Output json:

{ "index" : { "_index" : "products", "_type" : "post", "_id" : "0" } }
{"id":"b000jz4hqo","title":"clickart 950 000 - premier image pack (dvd-rom)","description":"","manufacturer":"broderbund","price":29.99}
{ "index" : { "_index" : "products", "_type" : "post", "_id" : "1" } }
{"id":"b0006zf55o","title":"ca international - arcserve lap/desktop oem 30pk","description":"oem arcserve backup v11.1 win 30u for laptops and desktops","manufacturer":"computer associates","price":19.99}

results matching ""

    No results matching ""