Search code examples
javascriptarraysobject

Convert contents of object to one entry in array


I have an object and I would like to convert the contents of the object as a string to first array element value , key of the array will be 0

example: I have

const obj1= {
all-exception: []
root-exception: "java.util.concurrent.CompletionException\n"
timestamp: 1671113624318
truncated: false
}

I would like to see the result as :

const obj2= 
0: "{\"all-exceptions\":[],\"root-exception\":\"java.util.concurrent.CompletionException\\n\",\"timestamp\": 1671113624318,\"truncated\":false}”

Solution

  • You can use the

    const obj = {
      'all-exception': [],
      'root-exception': "java.util.concurrent.CompletionException\n",
      timestamp: 1671113624318,
      truncated: false
    };
    
    console.log([JSON.stringify(obj)]);

    JSON.stringify method: