Page 1 of 1

How to update json value or if it empty do insert ?

Posted: Tue Oct 08, 2024 7:07 am
by hdt888
I already have a json file, I want to update the value for member.
Or if this member does not exist, insert this new member and insert the new value to it.

update

Code: Select all

{
"done": true
}
to:

Code: Select all

{
"done": false
}

Re: How to update json value or if it empty do insert ?

Posted: Tue Oct 08, 2024 1:27 pm
by Kulrom
like this

Code: Select all

json$ = ~"{\"done\" : true}"

If ParseJSON(0, json$)
	Debug json$
	SetJSONBoolean(GetJSONMember(JSONValue(0), "done"), #False)
	json$ = ComposeJSON(0)
	FreeJSON(0)
	Debug "---------------"
	Debug json$ ;    {"done":false}
EndIf

Re: How to update json value or if it empty do insert ?

Posted: Tue Oct 08, 2024 4:44 pm
by Little John
hdt888 wrote: Tue Oct 08, 2024 7:07 am I already have a json file, I want to update the value for member.
Or if this member does not exist, insert this new member and insert the new value to it.

Code: Select all

Input$ = "{" + Chr(34) + "done" + Chr(34) + ": true" + "}"
; Input$ = "{" + Chr(34) + "x" + Chr(34) + ": 10" + "}"

ParseJSON(0, Input$)

member = GetJSONMember(JSONValue(0), "done")
If member = 0
   member = AddJSONMember(JSONValue(0), "done")
EndIf   

SetJSONBoolean(member, #False)

Debug ComposeJSON(0)

Re: How to update json value or if it empty do insert ?

Posted: Fri Oct 11, 2024 2:15 pm
by hdt888
ok, thank you. it's working fine.