# Reading and writing application settings as JSON

**URL:** <https://forum.juce.com/t/reading-and-writing-application-settings-as-json/29947>\
**Category:** General JUCE discussion\
**Created:** [October 19, 2018, 9:15pm UTC](https://forum.juce.com/t/reading-and-writing-application-settings-as-json/29947 "2018-10-19T21:15:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![doriancheah](https://avatars.discourse-cdn.com/v4/letter/d/67e7ee/32.png) [@doriancheah](https://forum.juce.com/u/doriancheah)\
**Post date:** [October 19, 2018, 9:15pm UTC](https://forum.juce.com/t/reading-and-writing-application-settings-as-json/29947/1 "2018-10-19T21:15:25Z")

</div>

I’m new to C++ and JUCE. I want to read and write my application settings from a file. I’m using a simple JSON structure:

```
{"property1":"value1", "prop2":"value2"}

```

I can use JSON class to parse the file and access the properties through a var object, but I see no way to set new values, except by creating a DynamicObject like this:  
config = JSON::parse (“thefile.json”);  
auto obj (config.getDynamicObject());  
obj-\>setProperty(“projectName”, “This is the new project name”);

Seems like an extra step. Is there a better way of doing this? Thanks for any suggestions!

---

<div class="post-metadata">

**Author:** ![alassandro](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/alassandro/32/4297_2.png) [@alassandro](https://forum.juce.com/u/alassandro)\
**Post date:** [October 19, 2018, 9:23pm UTC](https://forum.juce.com/t/reading-and-writing-application-settings-as-json/29947/2 "2018-10-19T21:23:52Z")

</div>

You may want to check out [juce::PropertiesFile](https://docs.juce.com/master/classPropertiesFile.html), it lets you manage a properties file as a `juce::PropertySet`. With the JSON approach you probably will be constrained by having to use `getDynamicObject()`

---

<div class="post-metadata">

**Author:** ![doriancheah](https://avatars.discourse-cdn.com/v4/letter/d/67e7ee/32.png) [@doriancheah](https://forum.juce.com/u/doriancheah)\
**Post date:** [October 19, 2018, 11:56pm UTC](https://forum.juce.com/t/reading-and-writing-application-settings-as-json/29947/3 "2018-10-19T23:56:55Z")

</div>

Thanks! I ended up using DynamicObject to do this because I can’t figure out where to instantiate the PropertiesFile object. I wanted it to be a member of my Config class and have the class read/write the properties and manage them for the rest of the application. It has been hard to find examples of relevant code online!

---

<div class="post-metadata">

**Author:** ![matkatmusic](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@matkatmusic](https://forum.juce.com/u/matkatmusic)\
**Post date:** [October 20, 2018, 10:32am UTC](https://forum.juce.com/t/reading-and-writing-application-settings-as-json/29947/4 "2018-10-20T10:32:19Z")

</div>

Look in the Useful Tools category on the forum for my ScopedValueSaver class

---

<div class="post-metadata">

**Author:** ![adamwilson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/adamwilson/32/1387_2.png) [@adamwilson](https://forum.juce.com/u/adamwilson)\
**Post date:** [October 22, 2018, 9:22pm UTC](https://forum.juce.com/t/reading-and-writing-application-settings-as-json/29947/5 "2018-10-22T21:22:30Z")

</div>

Yeah, JUCE and JSON are not the best of friends… There’s a bit of hoop-jumping required to create and manipulate JSON structures using `var` and the underlying `DynamicObject` and `NamePropertySet`. I created a helper method that might be useful:

```cpp
/** Create a new var property, and either add it to an existing var object
    or create a new one. Returns the new or updated var object.
    @param name property name
    @param value property value
    @param varToAddTo var to add property to, defaults to empty
*/
static var propertyAsVar(const String& name, const var &value, var varToAddTo = var())
{
    DynamicObject* obj = nullptr;
    if (varToAddTo.isVoid())
        obj = new DynamicObject();
    else
        obj = varToAddTo.getDynamicObject();

    NamedValueSet& properties = obj->getProperties();
    properties.set (name, value);
    return var (obj);
}

```

If you want to use JSON heavily in your project it might be good to look at e.g. [JsonCpp](https://github.com/open-source-parsers/jsoncpp)
