Append of JSON to the payload

Hi,

I am reading through different files in an loop.Now I need to add the content of all files(JSON) and send the payload.

But I am getting the output as below with {} appended in the front and \ in the results.

{}{\"Complexity\":[\"Medium\",\"Low\",\"High\"]}"{"color":["Red","Blue","Green"]}

Below is the code:

'use strict';
const fs = require('fs');
var http = require('http');
var url = require('url');
var path = require("path");
var payload = {};
function requestHandler(request, response) {
  var files =[file1.json,file2.json]
  for(item in files){
    filename =files[item];
    if(item=== 0)
    {
      payload = JSON.parse(fs.readFileSync(filename, 'utf-8'));
    }
    else
    {
      filecontent = JSON.parse(fs.readFileSync(filename, 'utf-8'));
      payload = JSON.stringify(payload).concat(JSON.stringify(filecontent));
    }
  }
  response.writeHead(200, {"Content-Type": "application/json"});
  response.end(JSON.stringify(payload, null, 2) + '\n');
}


var svr = http.createServer(requestHandler);
svr.listen(9000, function() {
  console.log('Node HTTP server is listening');
});

Expected output is without {} in the front and without \ in the results.

I also tried using stringbuffer as below but got an error that it could not find string buffer in Apigee

	var StringBuffer =require("stringbuffer");
Solved Solved
0 3 1,265
1 ACCEPTED SOLUTION

You can implement the same thing without the if/else condition to avoid duplicating your fileread/parse line

If you're wanting to return a valid json structure where the content of each file is returned in a combined json array it would be something like...

var fileItems = [];
for (i in files) {
	var filename = files[i];
	var fileJson = JSON.parse(fs.readFileSync(filename, 'utf-8'));
	fileItems.push(fileJson);
}


// your response building code here, using stringify only once
// JSON.stringify(fileItems)

View solution in original post

3 REPLIES 3

Hi there

I'm assuming you're executing this if/else block in a loop.

If you look at your output, the empty braces is due to payload set to empty on a first read?

The second set appears to be stringified twice, causing the quotes to be escaped. (each execution of the loop is causing everything to be stringified again..)

Also do you really want to be concatenating in such a way? It's not producing valid json. I'm assuming you want to return an array of filecontent? Create an array, and push your filecontent to it.

I'd recommend doing any stringifying outside of the loop once youve built your final (valid) structure .

1. can you show us the entire loop?

2. confirm: is this code running in a nodejs target?

3. how many files will you read?

4. What is the expected output? you told us what you are actually seeing. can you give us a specific example of what you expect to see?

You can implement the same thing without the if/else condition to avoid duplicating your fileread/parse line

If you're wanting to return a valid json structure where the content of each file is returned in a combined json array it would be something like...

var fileItems = [];
for (i in files) {
	var filename = files[i];
	var fileJson = JSON.parse(fs.readFileSync(filename, 'utf-8'));
	fileItems.push(fileJson);
}


// your response building code here, using stringify only once
// JSON.stringify(fileItems)