Skip to main content

toPrettyString

Converts an object to a formatted JSON-object with indents.

tip
Returns string.

How to use

Can be used while logging:

var customer = {"firstName":"John","lastName":"Doe","id":5566,"cart":[{"itemName":"Fishing rod","price":432},{"itemName":"Fishing line","price":34}]}

Compare examples of logging objects using toPrettyString and without it:

  • log(customer):

    log(customer): {"firstName":"John","lastName":"Doe","id":5566,"cart":[{"itemName":"Fishing rod","price":432},{"itemName":"Fishing line","price":34}]}
  • log("Customer info:\n" + customer):

    [object Object]
  • log(toPrettyString(customer)):

    {
    "firstName": "John",
    "lastName": "Doe",
    "id": 5566,
    "cart": [
    {
    "itemName": "Fishing rod",
    "price": 432
    },
    {
    "itemName": "Fishing line",
    "price": 34
    }
    ]
    }
  • log("Customer info:\n" + toPrettyString(customer)):

    Customer info:
    {
    "firstName": "John",
    "lastName": "Doe",
    "id": 5566,
    "cart": [
    {
    "itemName": "Fishing rod",
    "price": 432
    },
    {
    "itemName": "Fishing line",
    "price": 34
    }
    ]
    }