LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Clean-up Returned JSON string to a readable format

Hello LabVIEW community:  

 

I was searching for such a tool but to no avail. I can probably write this up myself but why re-invent the wheel if it is already there.

 

The Challenge

--------------------

Each time I submit clean JSON to HTTP web service VIs, the returned body string looks messy featuring no return and/or line-feed characters to help organise those curly brackets.

 

>>> The attached shows what I am trying to achieve. i.e

 

GOAL

----------

To convert a messy raw JSON string to a clean and readable one featuring exact same-JSON string.

 

        . . . Is there a VI for this somewhere? See the before and after goal image attached.

 

 

Anthony

0 Kudos
Message 1 of 10
(4,089 Views)

Not LabVIEW but online solution.  I've used JSON Editor Online.   https://jsoneditoronline.org

 

Paste it into the left pane.

Click to send it to the right pane. (Which lets you edit things if you wanted.)

Click to send it back to the left pane.

Copy it out of the left pane.

 

The transfer back to the left plane cleans it up.

0 Kudos
Message 2 of 10
(4,052 Views)

The term you want is "pretty print" or "prettify" and if you search for that, you can find this, for instance:

 

https://www.vipm.io/package/felipefoz_lib_json_pretty_printer/

 

I believe the JSON toolkit on LAVA also has a pretty print function.


___________________
Try to take over the world!
0 Kudos
Message 3 of 10
(4,024 Views)

JSONtext has a "Reformat" vi that has a optional input to pretty print.  It also installs a "JSON Pretty Print" custom probe that makes debugging JSON much easier.

Message 4 of 10
(4,019 Views)

Well I did not want to add a library just for this, and I always wondered what would go into writing one.

So after some research, I found this SO answer and adapted it.

 

It's a little buggy on the edges but it still does a pretty good job if the goal is only to "pretty" display.

At some point, I will try and write a better one, but for now this will do.

 

Hope it helps someone

0 Kudos
Message 5 of 10
(2,990 Views)

Fixed some bugs

Message 6 of 10
(2,976 Views)

Added check if input json string is already prettified to kmurphs version (LV2021).

0 Kudos
Message 7 of 10
(2,873 Views)

@Anthony_L wrote:

 

The Challenge

--------------------

Each time I submit clean JSON to HTTP web service VIs, the returned body string looks messy featuring no return and/or line-feed characters to help organise those curly brackets.


The HTTP VIs have no idea about what the contents in the HTTP response is. It could be JSON, XML, JAVA, random text or fully binary data. So they simply return whatever the server responded with. Sometimes the server may have an extra header field that you can add to the request to ask it to return prettyfied data, or it could be an URL parameter you need to add. By default most servers tend to return concise data to reduce the amount of bytes that need to be transfered. Also there is of course always a very big chance that whatever the server may consider "pretty" is totally disgusting to the end user. Those tab wars in source code files do exist. 😁

Rolf Kalbermatter
My Blog
0 Kudos
Message 8 of 10
(2,865 Views)

Hey, @hunkel can you save for a previous version of LV and re-upload?

I currently only have LV2020.

0 Kudos
Message 9 of 10
(2,843 Views)

Hi, here is the LV2015 version.

0 Kudos
Message 10 of 10
(425 Views)