Pass JSON data to program

Just starting out? Need help? Post your questions and find answers here.
Maz
New User
New User
Posts: 1
Joined: Sat Jun 15, 2024 1:45 pm

Pass JSON data to program

Post by Maz »

Hey, I try to pass JSON data as an argument to a program using RunProgram().
The JSON data for example is {"date_id":"20240511","date_type":"1"}.

It seems I can not pass the quotes correctly. Based on what is possible in the shell, I would write:

Code: Select all

Arguments$ = ~"api get getHouseholdStoragePsReport '{\"date_id\":\"20240511\",\"date_type\":\"1\"}'"
p = RunProgram(Command$, Arguments$ ; ...
Here however it seems the single quotes gets passed as part of the fourth argument.
PBJim
Enthusiast
Enthusiast
Posts: 296
Joined: Fri Jan 19, 2024 11:56 pm

Re: Pass JSON data to program

Post by PBJim »

Welcome to the forum. You need to use the UnescapeString() function... https://www.purebasic.com/documentation ... tring.html

Also add the mode #PB_String_EscapeJSON as the second parameter of the function.

I'm sorry I can't test it and give you an example, because I use PB 6.00 which doesn't include #PB_String_EscapeJSON.
verifiablefile
New User
New User
Posts: 2
Joined: Mon Jun 17, 2024 6:03 am

Re: Pass JSON data to program

Post by verifiablefile »

Maz wrote: Sat Jun 15, 2024 3:10 pm Hey, I try to pass JSON data as an argument to a program using RunProgram().
The JSON data for example is {"date_id":"20240511","date_type":"1"}.

It seems I can not pass the quotes correctly. Based on what is possible in the shell, I would write:snow rider

Code: Select all

Arguments$ = ~"api get getHouseholdStoragePsReport '{\"date_id\":\"20240511\",\"date_type\":\"1\"}'"
p = RunProgram(Command$, Arguments$ ; ...
Here however it seems the single quotes gets passed as part of the fourth argument.
Passing JSON data as an argument to a program can be tricky because JSON often includes quotes and special characters that need to be properly escaped. The method for escaping quotes and special characters depends on the shell and programming environment you're using.
Henjamin
New User
New User
Posts: 1
Joined: Wed Jul 02, 2025 8:09 am

Re: Pass JSON data to program

Post by Henjamin »

Maz wrote: Sat Jun 15, 2024 3:10 pm Hey, I try to pass JSON data as an argument to a program using RunProgram().
The JSON data for example is {"date_id":"20240511","date_type":"1"}.

It seems I can not pass the quotes correctly. Based on what is possible in the shell, I would write:

Code: Select all

Arguments$ = ~"api get getHouseholdStoragePsReport '{\"date_id\":\"20240511\",\"date_type\":\"1\"}'"
p = RunProgram(Command$, Arguments$ ; ... 
drive mad
Here however it seems the single quotes gets passed as part of the fourth argument.
I considered using quotation marks for the entire argument and escaping the inner quotation marks.
User avatar
Caronte3D
Addict
Addict
Posts: 1361
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Pass JSON data to program

Post by Caronte3D »

If you only need to pass an argument, and the Command$ program is yours, you can send the json as base64 then come back to the original json string on the Command$ program.
Post Reply