I’ve been banging my head against the wall for the past ~5 hours and have gotten absolutely no-where on this. Can anyone show a method that works? testing on 6.49.7…
I am trying to use fetch to pull down the contents of an array, and then retrieve elements of it for later use. It looks like it should work, but the best I can tell it’s not recognizing the data in an array format and treating it like a single string, and thus no element data gets returned.
This server returns the following string (which I can modify formatting if needed to solve this, and I tried it with and without the {} and found no difference)
This much appears fine, but the problem is when I try to retrieve any element of this array I get a blank output.
:local strData {[:toarray ([/tool fetch mode=https output=user url="https://myserver.com/getvalues" as-value ]->"data")]}
:put $strData
:put "The Password is $($strData->"pass")"
[admin@RouterOS] /system script> run test
ssid="network";username="test@domain";pass="mypass123"
The Password is
However when I run the same thing off data that I populate directly from the script it works
:local strData {ssid="network";username="testuser";pass="mypass123"}
:put "The Password is $($strData->"pass")"
[admin@RouterOS] /system script> run test
The Password is mypass123
Obviously any read from file, is a string.
Obviously :toarray can convert only string to simple index array ( 1,2,3,4,5,6 )
Obviously if you pass a string that is not one array, but script syntax ( {ssid=“network”;…;pass=“mypass123”} ), this do not convert syntax into array…
Obviously is incorrect syntax :local strData {[ … ]}
If url is provided, mode is useless
Readed from file, or declared as string, for this example is equal:
As posted that had a small typo, but I fixed the unescaped " in front of $strData and that code doesn't work even running on the local string. I also tried it on the downloaded data using both escaped " and straight " with no difference....
:local strData "{ssid=\"network\";username=\"test@domain\";pass=\"mypass123\"}"
:put $strData
:local arrData [[:parse ":local temp \$strData; :return \$temp"]]
:put "The Password is $($arrData->"pass")"
:put "The Network is $($arrData->"ssid")"
:put [:typeof $arrData]
:put [:len $arrData]
:foreach val in=$arrData do={
:put "Items: $val"
}
[admin@RouterOS] /system script> run test
{ssid="network";username="test@domain";pass="mypass123"}
The Password is
The Network is
nil
0
[admin@RouterOS] /system script>
ok, I don’t know why it worked this time, but I swear I pasted the original code and it didn’t work (and why I assumed that $strData needed escaped, because it stopped throwing an error on that line when I did)
but now it works!
This line is the key to making it work.
:local arrData [[:parse ":local temp $strData; :return \$temp"]]
:put "The Password is $($arrData->"pass")"