Skip to content
⌘ NSIS Forum Archive

read json without nsjson

16 posts

shekara#

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
Anders#
Works just fine
Section "Prepare"
InitPluginsdir
FileOpen $0 "$PluginsDir\temp.txt" w
FileWrite $0 '{"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 
shekara#
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#
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"
}
Anders#
Again, just works just fine for me:

Section "Prepare"
InitPluginsdir
FileOpen $0 "$PluginsDir\temp.txt" w
FileWrite $0 '{$\n'
FileWrite $0 '"DATE" : "2022-03-06 08:45:36 +0000",$\n'
FileWrite $0 '"BUILD_NUM" : "123",$\n'
FileWrite $0 '"RELEASE_STR" : "1234/56"$\n'
FileWrite $0 '}$\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?
shekara#
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.
Anders#
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.
shekara#
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.
Anders#
There is a way to attach files. You might have to zip it.

Worst case, put a hex dump on pastebin.
shekara#
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