0

In my React Native app I'm displaying modals that contain a printout of an object. I use the Alert.alert() method, which you need to pass a string to. Since I have an object I want to print, I pass JSON.stringify(obj) to the method. However, this prints the object like this:

{"prop1":"value1","prop2":"value2"}

Is there a way to print it like

{
  prop1: value1,
  prop2: value2
}

without looping through the keys of the object?

gkeenley
  • 6,088
  • 8
  • 54
  • 129

1 Answers1

0

Run it in the console. It will copy it to your clipboard.

const beautifyJSON = JSON.stringify({"prop1":"value1","prop2":"value2"}, null, "\t");

You can read about stringify parameters here

Alon Shmiel
  • 6,753
  • 23
  • 90
  • 138
  • Can you elaborate a bit on what this means? The reason I'm displaying text in modals is that I'm running a build of the app, so I can't attach a debugger or anything with a console. – gkeenley Jan 28 '22 at 21:07
  • JSON.stringify gets three parameters: the first one is required and the others are optional. The second one is replacer (select or filter the keys), and the third one is called space (insert space into the output object). – Alon Shmiel Jan 28 '22 at 21:22
  • 1
    @gkeenley Be aware that this method converts the object to a JSON string. It doesn’t keep functions for example – evolutionxbox Jan 29 '22 at 02:11