|
The database application that stores all the data I'm going to be using for my app can basically only export strings. So even if I have tables of data, they get written in CSV as "text;text;text" etc. (The database also spits out a ton of categories I don't want, and it's easier to delete them by deleting columns of a spreadsheet than using a texteditor; thus, the CSV export.) Either way, all the fields/properties get mixed around as well; for example:
My preference would be for that to read:
In this example, I also wanted to add a Here's the code I used, including the fixes I made to the incoming data to remove the ";"s and convert the population strings (originally in thousands-of-people units) into the correct numerical values. Here's the
With the following being what you might want the actual
So here's the restructuring template (you'll need to adjust the commented values to your specifications
Here's the simple interface I used to "activate" the rewrite (statedata.json is the name of the file with the original, unsorted JSON from the first code block above):
Hopefully, that template helps anyone who's looking to clean up a JSON file to make it easier to read. It's a bit brute-force, but it gets the job done, and once you've renamed everything to fit your app or code the first time, subsequent adjustments would just involve a copy-and-pastes. |
|
Or you tell it to
|
|
Just out of curiosity, why so much effort expended on getting a particular order of the JSON keys, when the order doesn't matter? Regardless of what order the keys are in, as long as your
Yeah, that helps with how it's displayed but it does nothing to change the order of the keys, which apparently is one of the things
|
|
@roosterboy There's a lot of (different types of) data in the app I'm preparing, and some of it isn't complicated enough to bother maintaining a whole database for--it's just easier to enter in a text list. And then it's just much easier for me to interact with the data if it's organized in an order where properties flow in the same manner I'd expect to read information presented in an encyclopedia entry or wikipedia page. That's how I build my Structs, so having the data ordered the same way just makes it easier to find what I need. 8^) Oh, and I forgot to mention this in the first post: the output text is compact JSON. I then use the free Mac app Boop to format it into readable form (and there are a multitude of json validators and such online that can do the same). |
|
@roosterBoy One example would be if you need to diff the codable text (without parsing it), e.g., to determine if A==B. |
|
That wouldn't require the keys to be in a particular order, just that the same order is used in both examples you are diffing. Not to mention there are online and command line tools that will diff two JSON strings regardless of key orders. |
TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.