Old 14th March 2022, 07:09   #1
shekara
Junior Member
 
Join Date: Oct 2020
Posts: 47
read json without nsjson

in NSIS, I don't find native JSON support and I cannot use nsjson plugin. I have to use string instructions to read a json property.
{
"DATE" : "****",
"BUILD_NUMBER" : ***,
"RELEASE_STRING" : "***"
}

How do I read these json properties and store these values in the variable
shekara is offline   Reply With Quote
Old 15th March 2022, 02:20   #2
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,538
StrCpy+StrLen+StrCmp in a custom parser.

Why can't you use a plug-in?

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 15th March 2022, 10:46   #3
shekara
Junior Member
 
Join Date: Oct 2020
Posts: 47
plugin didnt work and last time in forum it was discussed it had some issues
shekara is offline   Reply With Quote
Old 15th March 2022, 23:32   #4
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,538
Works just fine
PHP Code:
Section "Prepare"
InitPluginsdir
FileOpen 
$"$PluginsDir\temp.txt" w
FileWrite 
$'{"DATE" : "1234","BUILD_NUMBER" : 5678,"RELEASE_STRING" : 9.10}'
FileClose $0
SectionEnd  

Section
nsJSON
::Set /file "$PluginsDir\temp.txt"
nsJSON::Get "DATE" /end
Pop 
$0
nsJSON
::Get "BUILD_NUMBER" /end
Pop 
$1
nsJSON
::Get "RELEASE_STRING" /end
Pop 
$2
MessageBox mb_ok 
$0,$1,$2
SectionEnd 

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 17th March 2022, 03:10   #5
shekara
Junior Member
 
Join Date: Oct 2020
Posts: 47
I have a predetermined json file given like test.json which contains json properties. I cannot write it, but I am supposed to read values from predetermined json files. for that it does not work. if I have test.json in temp dir, i cannot read it.
shekara is offline   Reply With Quote
Old 17th March 2022, 10:18   #6
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,538
And what does this non-working file look like?

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 18th March 2022, 03:41   #7
shekara
Junior Member
 
Join Date: Oct 2020
Posts: 47
{
"DATE" : ,
"BUILD_NUM" : ,
"RELEASE_STR" : ""
}

ABove would be format of given json file
shekara is offline   Reply With Quote
Old 18th March 2022, 10:58   #8
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,538
That is not valid JSON. Empty values should be "", null, or not present at all.

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 18th March 2022, 14:37   #9
shekara
Junior Member
 
Join Date: Oct 2020
Posts: 47
my fault...I wanted to hide few info. the file will be something like this

{
"DATE" : "2022-03-06 08:45:36 +0000",
"BUILD_NUM" : "123",
"RELEASE_STR" : "1234/56"
}
shekara is offline   Reply With Quote
Old 19th March 2022, 16:12   #10
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,538
Again, just works just fine for me:

PHP Code:
Section "Prepare"
InitPluginsdir
FileOpen 
$"$PluginsDir\temp.txt" w
FileWrite 
$'{$\n'
FileWrite $'"DATE" : "2022-03-06 08:45:36 +0000",$\n'
FileWrite $'"BUILD_NUM" : "123",$\n'
FileWrite $'"RELEASE_STR" : "1234/56"$\n'
FileWrite $'}$\n'
FileClose $0
SectionEnd  

Section
nsJSON
::Set /file "$PluginsDir\temp.txt"
nsJSON::Get "DATE" /end
Pop 
$0
nsJSON
::Get "BUILD_NUM" /end
Pop 
$1
nsJSON
::Get "RELEASE_STR" /end
Pop 
$2
MessageBox mb_ok 
$0$\n$\n$1$\n$\n$2
SectionEnd 
Can you confirm if my code works for you?

Perhaps your file has a special encoding? UTF16? UTF8 with BOM?

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 20th March 2022, 04:33   #11
shekara
Junior Member
 
Join Date: Oct 2020
Posts: 47
the code works. but the problem is DATE, BUILD, RELEASE values keep changing evry day and I wouldn't know what the values are to write it into text file. At run time in my installer, i have to read from a .json file and output it into my file.
shekara is offline   Reply With Quote
Old 20th March 2022, 10:16   #12
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,538
Section "Prepare" is not something you would use in your .nsi, it is just there to generate a example JSON file. The other section only cares about the names, not the value.

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 22nd March 2022, 10:46   #13
shekara
Junior Member
 
Join Date: Oct 2020
Posts: 47
for some strange reasons, this does not seem to work for you. I could have attached my file. But there is no option to attach it.
shekara is offline   Reply With Quote
Old 22nd March 2022, 11:48   #14
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,538
There is a way to attach files. You might have to zip it.

Worst case, put a hex dump on pastebin.

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 22nd March 2022, 12:39   #15
shekara
Junior Member
 
Join Date: Oct 2020
Posts: 47
got to know the reason... I have a key/property with an empty value... because of empty value "ABC" : [], it is not showing other values as well

guess it does not support empty arrays.

even in a single empty array, the plugin does not read the entire json file. that's unfortunate
shekara is offline   Reply With Quote
Old 23rd March 2022, 09:13   #16
shekara
Junior Member
 
Join Date: Oct 2020
Posts: 47
Anders, can you please elaborate on StrCpy+StrLen+StrCmp for json files
shekara is offline   Reply With Quote
Reply
Go Back   Winamp & Shoutcast Forums > Developer Center > NSIS Discussion

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump