(How to) Convert JSON array in string variable to Array?

sampleuserhere

Active member
Everytime I parse an array with JSON parse, I get "[1,2,3,4]". How do I do the reverse directly? Is this possible?

For the time being, I could parse the string into dictionary with JSON parse. Then I have the array in dictionary variable. However I couldn't find a way to turn it into an array directly.

TIA.
 

Quidn

Passionate Member
I believe there would be particular features in the future, but currently the only difference between Array and Dictionary I could found was that only numbers are allowed in array index when if manually set an entry.

Is there any significant difference between them in your use case? If so please let me know if you don't mind. It's not to prove they are same thing, but I just want to find and see in which cases they might different.
 

Quidn

Passionate Member
Hmm, I don't think my request was. The pic you had shared showed that the array is still inside the dictionary variable.
That's because the source JSON has that array in a parent object. If so, it's unclear to me what you want.

Oh, did you mean that you wanted to select the output to an array type variable?
...I couldn't thought for that because that's not a practically feasible option for so many reasons.
 

sampleuserhere

Active member
did you mean that you wanted to select the output to an array type variable?
...I couldn't thought for that because that's not a practically feasible option for so many reasons.

Exactly.

Well maybe it is, I assumed that MD could do it the other way around and I wasn't just aware of it.

Anyway, I've just remembered that the mod moved this thread to "Feature Request" subforum, I originally posted this in "Help" subforum. So this wasn't intended to be a feature request in the first place.
 

Quidn

Passionate Member
Exactly.

Well maybe it is, I assumed that MD could do it the other way around and I wasn't just aware of it.

Anyway, I've just remembered that the mod moved this thread to "Feature Request" subforum, I originally posted this in "Help" subforum. So this wasn't intended to be a feature request in the first place.
o_O I see. I don't understand why someone moved this thread to here.

Think about it. Since MacroDroid doesn't support dynamic type changing for variables and overwhelming majority of JSON is objects, it will just adds unnecessary complexity. Furthermore it can be done by "Split to array" after trim the first and last characters although it may requires additional unescaping. In my opinion the practicality is likely minimal compared to the considerations for implementation.

However, I guess it might be relatively little more simpler if the output is set to an entry of dictionary or array, which is currently the only way to dynamically sets variable type. But still there are several things to be considered and I doubt it'll be really needed to.
 

sampleuserhere

Active member
Try the following syntax in Javascript and MD.

Code:
 ar = [1,2,3,4];
arstr = JSON.stringify(ar);
arr = JSON.parse(arstr);
final = JSON.stringify(arr);
console.log(fin
al);

MD will output {"entries":[1,2,3,4]}. It doesn't look natural at all that we get an object after the second stringify (JSON output) in MD.

May not be necessary but it's just so strange that the conversion couldn't happen both ways in MD. That's why I wasn't sure that it wasn't possible to begin with.
 

Quidn

Passionate Member
MD will output {"entries":[1,2,3,4]}. It doesn't look natural at all that we get an object after the second stringify (JSON output) in MD.

May not be necessary but it's just so strange that the conversion couldn't happen both ways in MD. That's why I wasn't sure that it wasn't possible to begin with.
To me that looks very natural and even think that's more suitable to MacroDroid(Actually I didn't tried but I know what the code results, and didn't do on MacroDroid too but I assumed based on your explanation).

You will easily guess why when you search actual JSON usage cases or tutorials. :)

[1,2,3,4]
Although it's not a grammatically incorrect syntax, but generally it'll not be considered as a JSON string. Which means, even though it must considered as a valid JSON, but likely not easily identified as a JSON string at a glance.

I think you will not likely facing a ❴JSON string❵ that you actually need to parse which doesn't start and end with curly braces, unless you make your own or do something very experimental.
 
Last edited:

sampleuserhere

Active member
I think you will not likely facing a ❴JSON string❵ that you actually need to parse which doesn't start and end with curly braces, unless you make your own or do something very experimental.

Well who knows. At least I could always get around this by outputting the "entries" key instead.
 

sampleuserhere

Active member
Lmao, you could always leave it alone but you started your thing yourself anyway. 🤣 You got curious today and All I did was elaborating why I started the thread.

Anyhow, my question was already resolved and it was just "Is this possible or not?", @Endercraft answered "Nope" and my post was moved to the request. This was supposed to be like that.
 

Quidn

Passionate Member
Lmao, you could always leave it alone but you started your thing yourself anyway.
Again, no. Do you really think so?



Well who knows.
Can't you think that makes me to explain? Just understand it that way, or ignore if you don't want to so. Why are you saying like that even though I added several conditions? Because there are multiple conditions that cannot be judged objectively, there cannot be disagreement, logically.
I think you will not likely facing a ❴JSON string❵ that you actually need to parse which doesn't start and end with curly braces, unless you make your own or do something very experimental.



You got curious today and All I did was elaborating why I started the thread.
Again and again, no.
Well who knows.
This doesn't related to that at all.



Anyhow, my question was already resolved and it was just "Is this possible or not?", @Endercraft answered "Nope" and my post was moved to the request. This was supposed to be like that.
You don't have to repeat again since I already said like the below.
o_O I see. I don't understand why someone moved this thread to here.



you could always leave it alone but you started your thing yourself anyway.
It's annoying to see disagreements that don't make sense.



And again and again and again, you make me to explain. Although it's my choice to explain, but you should know that you're lead to so. Did you really don't understand?

That's okay to do as you want, but I beg you at least please realize that you led me repeatedly.
 
Top