1

I have this string:

"{filter: [{\"attribute\":\"quoteitemnum\",\"filterOperation\":\"EQUALS\",\"expressionValue\":\"581-2019-2\"}]}"

im trying to have nice format json in textarea:

 <textarea [ngModel]="request | json" disabled></textarea>

But this is not working.

I tried this also:

const myObjStr = JSON.stringify(this.request);
this.obj.request = JSON.parse(myObjStr);



  <textarea [ngModel]="obj.request" disabled></textarea>

and

<textarea [ngModel]="obj.request | json" disabled></textarea>

But its not working. Any suggestion?

2
  • Your string has some backslashes. Have you tried removing them? Commented Oct 31, 2019 at 14:14
  • Your string it is little bit complex, as @Lok said did you try to remove them ? Commented Oct 31, 2019 at 14:15

4 Answers 4

2

I believe the string is an invalid JSON string. When I used the string below it works fine with the json pipe. The filters key is not escaped (\"filter\")

"{ \"filter\":[{ \"attribute\":\"quoteitemnum\",\"filterOperation\":\"EQUALS\",\"expressionValue\":\"581-2019-2\"}]}"

https://stackblitz.com/edit/angular-czcsuj

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

3 Comments

how can i make it valid because im getting it from backend like that i cannot change it on backend
You could do a string replace string.replace('filter', '\"filter\"').
My suggestion is to fix the issue on the backend. If you absolutely can't, you can follow @Sam 's suggestions or you could parse out the array value that is being assigned to filter and then rebuild the object. Something like this.... stackblitz.com/edit/angular-yjzteb
0

I think the given string should be something like below,

"{ \"filter\":[{ \"attribute\":\"quoteitemnum\",\"filterOperation\":\"EQUALS\",\"expressionValue\":\"581-2019-2\"}]}"

You didn't escape the filter. And if the json string is correct, the json pipe should work fine.

<textarea style="background-color:black;color:white;"rows="30" cols="50">  
  {{rapidPageValue | json}}                           
</textarea>

Instead of textarea you can also make use of pre tag

  `<pre>{{rapidPageValue | json}}</pre>`

1 Comment

im getting string like that from backend and i cannot modify it to get "valid" json
0

you can parse json string and use tag and json pipe

hope this helps...

https://stackblitz.com/edit/angular-rafyyw?file=src/app/app.component.ts

Comments

-1

This should work:

ngOnInit() {
this.request =
  "{ \"filter\":[{ \"attribute\":\"quoteitemnum\",\"filterOperation\":\"EQUALS\",\"expressionValue\":\"581-2019-2\"}]}";

const myObjStr = JSON.stringify(JSON.parse(this.request));
this.request = JSON.parse(myObjStr);

}

Like @Jason White said, wrap the string with JSON.parse before doing JSON.stringify

I hope it helps.

Comments

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.