Search code examples
jsonangulartypescriptstringify

How to use 'safe-json-stringify' in Angular 4 Application?


My requirement is to convert JSON object to string. For this, simply JSON.stringify() doesn't work here because it contains circular references. I searched on the internet then I found a package which does this work for me. But in the usage of the package it is mentioned like const safeJsonStringify = require('safe-json-stringify');. But here the problem is that in Angualar 4 doesn't support require() function anymore. In latest versions of Angular (from version 2), we have import statements. It is like if we have a class is exported in the package then we can directly import that class from the package.

Any suggestions ??

Thanks in advance!!


Solution

  • Try something like:

    import * as SafeJsonStringify from 'safe-json-stringify'
    

    This way your import statement can act (in some ways) as a typical require(), as shown in the documentation.

    Note: SafeJsonStrinfigy is a value of your choice: you can name it as you like.