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

Just starting out? Need help? Post your questions and find answers here.
hdt888
User
User
Posts: 47
Joined: Sun Jul 07, 2024 8:42 am

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

Post 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
}
PB 5.x + 6.x + Win10. Feel the ...Pure... Power.
Kulrom
User
User
Posts: 16
Joined: Thu Sep 07, 2023 6:07 am

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

Post 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
I love programming languages that start with the letter "P":
Python, Pascal and ... PureBasic! :)
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

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

Post 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)
hdt888
User
User
Posts: 47
Joined: Sun Jul 07, 2024 8:42 am

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

Post by hdt888 »

ok, thank you. it's working fine.
PB 5.x + 6.x + Win10. Feel the ...Pure... Power.
Post Reply