MATLAB Answers

Convert Struct to Readable JSON(Pretty Print)

122 views (last 30 days)
ILoveMATLAB
ILoveMATLAB on 4 Sep 2019
Answered: Gaurav Garg on 20 Nov 2019
Every struct I pass to jsonecode gets converted to a 1 by N char array with no newline chacters. Therefore, when I export the json as to a text file I only have one line. The file is not easly readble because I have to scroll the JSON text horizontaly. How can I make jsonencode's output easily readable(pretty print)?
I get this.
{ "firstName": "John", "lastName": "Smith", "age": 25, "address": { "streetAddress": "3 Apple Hill Dr", "city": "Natick", "state": "MA", "postalCode": "01760" }, "phoneNumber": [ { "type": "home", "number": "123 456 7890" }, { "type": "cell", "number": "098 765 4321" } ] }
I want something like this.
{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address":
{
"streetAddress": "3 Apple Hill Dr",
"city": "Natick",
"state": "MA",
"postalCode": "01760"
},
"phoneNumber":
[
{
"type": "home",
"number": "123 456 7890"
},
{
"type": "cell",
"number": "098 765 4321"
}
]
}

  0 Comments

Sign in to comment.

Answers (1)

Gaurav Garg
Gaurav Garg on 20 Nov 2019
Hi,
You can find a function which can help you resolve the problem you are facing in this file.

  0 Comments

Sign in to comment.

Products


Release

R2019a

Translated by