0

I dont understand, what the return value is back to as string, while the console.log is showing perfect formate, which required.

function syntaxHighlight(json) {
            json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
            return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
                var cls = 'number';
                if (/^"/.test(match)) {
                    if (/:$/.test(match)) {
                        cls = 'key';
                    } else {
                        cls = 'string';
                    }
                } else if (/true|false/.test(match)) {
                    cls = 'boolean';
                } else if (/null/.test(match)) {
                    cls = 'null';
                }
                return match ;
            });
        }
        var obj = { a: 1, 'b': 'foo', c: [false, 'false', null, 'null', { d: { e: 1.3e5, f: '1.3e5' } }] };
        var str = JSON.stringify(obj , undefined, 4);

        console.log(syntaxHighlight(str));
        $scope.jsonText = syntaxHighlight(str);

HTML

  <form name="form" ng-submit="vm.frmFormatJSON()">
            <div class="form-group" >
                <label>Format JSON</label>
                <input type="text" class="form-control " placeholder="{}" ng-model="vm.formatJSON">
            </div>
            <div class="text-center">
                <button type="submit" class="btn btn-primary" ng-click="formatJSON">
                    Look
               </button>
            </div>
        </form>

Console.log

enter image description here

Output

enter image description here

update as per @Fissio suggestion, its working perfect, but once I update

var str = JSON.stringify(vm.formatJSON, undefined, 4);

its again show in 1 line, also confuse why " " at the begning and end of the output.

5
  • Can you please add the html code that is displaying your output? Commented Feb 23, 2017 at 10:34
  • <td ng-bind-html="jsonText"></td> or <td>{{jsonText}}</td>, i tried both. Result should be like console.log , Commented Feb 23, 2017 at 10:35
  • So what's wrong with the output? Looks like it's just the same as the console.logged value...? Commented Feb 23, 2017 at 10:36
  • @Fissio, I mean the format, its just straight line Commented Feb 23, 2017 at 10:39
  • @faisal Oh okay, check the answer posted for a solution Commented Feb 23, 2017 at 10:48

1 Answer 1

1

Just use HTML <pre> tag and angular's json filter.

angular.module('app', [])
.controller('ctrl', function($scope) {
  $scope.data = {
    a: {
      b: [1, 2, 3],
      c: {
        nested: "json"
      }
    },
    lol: {
      5: "asd",
      asd: "5"
    }
  }
})
<html>
  <head>
    <script data-require="[email protected]" data-semver="1.6.0" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.0/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body ng-app="app" ng-controller="ctrl">
    <pre>{{data | json:4}}</pre>
  </body>
</html>

Sign up to request clarification or add additional context in comments.

8 Comments

can you check the updated part in my question. might you know this.
Don't use JSON.stringify at all for this; all you need to have is the raw JSON data.
my appologies, I am learning, I didnt get how to do this, any snippet?
There's the code snippet in my answer - forget the .stringify() altogether, the JSON data itself is what you want to have as the data - so since your data seems to be in a variable vm.formatJSON, your html should be <pre>{{vm.formatJson | json:4}}</pre>
yes, might I didnt explain well, there is a input field, what ever json enter, it just show the in format.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.