Skip to content
Advertisement

Is there a way to add curly brackets around a list of dictionaries already existing within a JSON file?

I currently have two JSONS that I want to merge into one singular JSON, additionally I want to add in a slight change.

Firstly, these are the two JSONS in question.

An intents JSON:

JavaScript

and an entities JSON:

JavaScript

The expected outcome is to create a JSON file that mimics this structure:

JavaScript

So what I am missing in my original files, is essentially:

JavaScript

and for the entities file:

JavaScript

at the start of each list of dictionaries.

Additionally, I would need to wrap the whole thing in curly braces to comply with json formatting.

As seen, the final goal is not just appending these two to one another but the file technically continues with some other JSON code that I have yet to write and deal with.

My question now is as follows; by what method can I either add in these words and the braces to the individual files, then combine them into a singular JSON or alternatively by what method can I read in these files and combine them with the changes all in one go?

The new output file closing on a curly brace after the entities list of dicts is an acceptable outcome for me at the time, so that I can continue to make changes and hopefully further learn from this how to do these changes in future when I get there.

TIA

Advertisement

Answer

JSON is only a string format, you can it load in a language structure, in python that is list and dict, do what you need then dump it back, so you don’t “add strings” and “add brackets”, on modify the structure

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement