Retrieving json from Gcp Bucket through Apigee using js file, but output is not formatted. Need help

Trying to retrieve json from Gcp Bucket, but json is having \n and \ before and after json attributes in entire json, Any help is appreciated to format to remove or replace \n and \ with whitespace or completely remove space from json and display.

js code is below : 

var retreivedStorageFile = context.getVariable("storage.file.retrieved");

context.setVariable(retreivedStorageFile, JSON.stringify(retreivedStorageFile));

print("retreivedStorageFile is: " + context.getVariable(retreivedStorageFile));

print("fileName is:" + context.getVariable("fileName"));

Need to use replace or remove function to remove \n and \ before and after attributes in json below : 

{
    "content""[\n    {\n        \"gmb_code\": \"ININDIND11$g\",\n        \"reputation_name\": \" Hospital at  St. Vincent\",\n        \"prediction_date\": \"2022-08-05\",\n  ]"}
0 5 185
5 REPLIES 5

I think you want to not call JSON.stringify(). That call is the thing that is escaping your newlines.

you have this

 

var retreivedStorageFile = context.getVariable("storage.file.retrieved");
context.setVariable(retreivedStorageFile, JSON.stringify(retreivedStorageFile));
print("retreivedStorageFile is: " + context.getVariable(retreivedStorageFile));
print("fileName is:" + context.getVariable("fileName"));

 

That thing on line 2... I think there are multiple problems there.

  1. the setVariable call takes a string as the first argument. It's the name of a variable. It should be something like this:
    
    context.setVariable('name-of-variable-here', JSON.stringify(retreivedStorageFile));
    
  2. There's no point in setting a context variable to the value of a thing that you just retrieved from another context variable.
  3. The value you set into the variable is JSON.stringified. You should omit that. the storage.file.retrieved is already JSON. You don't need to call JSON.stringify on it.

It seems to me that your JS code does not do anything significant. It retrieves from a context variable and then sets a context variable with the same value and prints some things. What are you REALLY trying to do?  what is your real goal?

 

context.setVariable(retreivedStorageFile, JSON.stringify(retreivedStorageFile)); I will remove,  my goal is to format output without \n and \ before and after attribute using below js with more enhancements. 

var retreivedStorageFile = context.getVariable("storage.file.retrieved");

print("retreivedStorageFile is: " + context.getVariable(retreivedStorageFile));

var retreivedStorageFile = context.getVariable("storage.file.retrieved");
var val = retreivedStorageFile.replace(/\\/g, " ");
print("retreivedStorageFile is: " + val);

print("fileName is:" + context.getVariable("fileName"));

But above code gives : 

LakshmiPavitra_0-1660938551938.png

 

 Hi Sir, @dchiesa1 Any help with above, please suggest.

I answered your other question.