Iterate var object


#1

Hi all
I have a JSON file as follows :

{
“Registration_Info”: {
“Registration_Key”: “b2b387652711393cb2”,
“Registration_Status”: “valid”,
“Customer_Name”: “xrousaios”
},
“Machine_Tags”: {
“System_DIR_ID”: “f00000002ab6d”,
“MAC_Addresses”: “fc-aa-14-32-26-b2/”,
“System_Product_ID”: “System_Product_ID”
},
“Activation_Status”: {
“Module_1”: {
“Module_Version”: “2.0.0”,
“Module_Status”: “valid”,
“Module_Activation_Date”: “2017-04-27T18:47:19.705+03:00”,
“Module_Expiration_Date”: “1900-01-00T00:00:19.705+553854:22”
}
“Module_2”: {
“Module_Version”: “2.0.3”,
“Module_Status”: “valid”,
“Module_Activation_Date”: “2017-04-27T18:47:19.705+03:00”,
“Module_Expiration_Date”: “1900-01-00T00:00:19.705+553854:22”
}
},
“Server_Comms”: {
“Last_Server_Date”: “2017-04-27T18:47:19.705+03:00”,
“Show_Comms_Warning”: “1”
}
}

This file is parsed into a var object via JSON::parse() function.
I have the need to modify just a simple named property, but, I could not design a suitable method for it.
At this point I have to :

  • iterate the var object to locate the named property
  • modify its value directly into the var ojbect
  • turn the var object again into JSON string

Any suggestion will be apreciated too much.

Thanks in advance

PS. I didn’t find any related topic in the forum that can met this issue


#2

parsing nested json


#3

It helps to spell out, what your structure in json is. In your case it is an array of objects. So you can do:

    var json;
    if (json.isArray()) {
        for (auto item : *json.getArray()) {
            if (auto object = item.getDynamicObject())
                if (object->hasProperty ("Valid"))
                    object->setProperty ("Valid", false);
        }
    }

Haven’t tested (too much work to create the json you have…), but should work

N.B. because you use getDynamicObject it doesn’t matter, if the var is a copy or not, because they reference a DynamicObject, which is a SharedObject referenced from the var.


#4

It is closely to my issuet, but focuses on how to add a property, rather than locating and changing it.

@daniel
No, it does not work. I think, an array in JSON is represented by a pair of brakets [].

@all
I feel that what I want is a recursively callled function, to traverse the whole var object, examning each contained DynamicObject or NamedProperty, if it contains the property that I need to modify.
I thing it can be seems like this:

bool modifyKeyFileEntry(var& Keys_Record,  String& Entry_Name,  String& Entry_Value)
	{
		for (auto obj : Keys_Record)
		{ 
			if (obj.isNode())
			{
				modifyCurrentEntry(var& Keys_Record, String& Entry_Name, String& Entry_Value);
			}
			else
			{
				if (obj.hasProperty(Identifier(Entry_Name)))
					obj.setProperty(Identifier(Entry_Name), Entry_Value);
			}
		}
	}

I am searching many hours the Juce forum and Juce code/documentation, but I could’nt find the pieces (code, functions or whatever. .) to implement a procedure like the one above.

Any thoughts are wellcome.

Thanks in advance

George


#5

I don’t think you searched hard enough:

constructing var and convert to json


#6

You are right, the wrong (or nonexistent) indent threw me off…

In that case you can work with DynamicObject::getProperties()

var json;
if (auto object = json.getDynamicObject)
    for (auto property : object->getProperties())
        if (auto sub = object [property].getDynamicObject())
            if (sub->hasProperty ("Valid"))
                sub->setProperty ("Valid", false):