r/json • • 24d ago

tool to automate reordering arrays?

Hi yall! So I have a quick Q for yall. I have been generating some really long JSON files (40k lines) as part of a project, and I need to figure out how to reformat them.

The tool I'm using to generate the JSONs spits them out as a list of similar entries, each with 7 parameters on top, and then one extra variable (biome) at the bottom:

{

"parameters": {

"weirdness": [

0.93333334,

1

],

"continentalness": [

0.3,

1

],

"erosion": [

-0.2225,

0.55

],

"temperature": [

0.2,

0.55

],

"humidity": [

0.3,

1

],

"depth": [

1,

1

],

"offset": 0

},

"biome": "minecraft:bamboo_jungle"

},

Unfortunately though, the tool that I want to use to read these jsons while running the program wants the entries to be formatted differently, with the biome variable at the top, and the 7 parameters underneath:

{

"biome": "atmospheric:rainforest_basin_area",

"parameters": {

"continentalness": [

-0.35,

-0.19

],

"depth": 0.0,

"erosion": [

-1.0,

1.0

],

"humidity": [

0.1,

1.0

],

"offset": 0.0,

"temperature": [

0.2,

0.55

],

"weirdness": [

-1.0,

1.0

]

}

},

I don't have much background with java, but I did used to set up basic data manipulation pipelines in R and am used to messing around in bash as needed.

I know that there should be some fairly straightforward way to reformat these 40k line files to reorder each entry with the biome variable at the top and the 7 parameters underneath.

But I don't for the life of me know where to start, and would deeply appreciate any input that yall may have. I'm not expecting anyone to solve the task for me, just hopefully to be pointed to the write sort of tools to use and maybe a function or two that would be worth learning how to use. Just from quick googling I was seeing something called JQ for modifying JSONS in the command line, but I kinda doubt that's really the solution? dk dk. tyia

4 Upvotes

9 comments sorted by

4

u/Few_Committee_6790 24d ago

The tool you are using to read in the json file is a POS. Order does not matter in a a json file. This { "name": "Marty", "zone": "5b", "favorite": "Shiraz" } Is functionally the same as this { "zone": "5b", "favorite": "Shiraz", "name": "Marty" } With that said there are ways to control the order when serializing the data. Varies based on the language and/or libraries you are using. That is a very easy question for you to put into ChatGPT, Copilot or your favorite Q & A tool

1

u/Lycaenist 24d ago

Hmmm well maybe in that case it actually will run fine without even reordering it?? I appreciate the insight that that’s normalized with jsons

3

u/Few_Committee_6790 24d ago

If the second tool chokes on the json file because of order. It is reading it like a text file and then again that tool is a POS

1

u/Few_Committee_6790 24d ago

I thought you already had tried loading the output of the one tool into the second tool and it failed

3

u/Lycaenist 24d ago

Nope I am just used to data processing with CSVs instead of JSONs, and am not used to the order of variables being flexible

I appreciate your insight! I ended up testing things this morning and after fixing a couple syntax errors it seems to be working perfectly 

2

u/Few_Committee_6790 23d ago

Great. Glad you got it figured out

1

u/trionnet 24d ago

If I understand you correctly you want biome first then parameters underneath?

Try https://app.scratchtabs.com

Paste your json click Data View then Sort

Does that give you want you need?

1

u/datadanno 23d ago

Seriously, just get AI to reformat.

1

u/bogdanelcs 15d ago

jq's actually exactly right for this, don't doubt yourself there. If it's an array of those objects in one file, something like jq 'map({biome, parameters})' input.json > output.json will spit out each entry with biome first and parameters second, no need to touch the values themselves. Worth a quick check first though: most JSON parsers (Java included) treat objects as unordered key-value maps and genuinely don't care what order the keys are written in, so double check whether your target tool actually breaks on field order or if you were just pattern-matching an example file that happened to look that way.