Skip to main content
Participant
September 30, 2021
Answered

Scripting for photoshop - JSON just returns [object Object]

  • September 30, 2021
  • 4 replies
  • 1924 views

Hello,

So I need to generate a big number of images from 1 image (like lottery tickets) that come from a set of numbers I have in a JSON file.

I have everything working, I just can't get the script to properly work with the JSON itself... For example, if I just run this code:

 

var data = [{"apple":{"size":5,"taste":4,"color":7}},
{"orange":{"size":8,"taste":4,"color":8}}]
alert(data);
alert(data[0]);
 
What I get on photoshop is just [object Object], instead of the data I put in the JSON object.
What I am doing wrong here, can anyone help me?
This topic has been closed for replies.
Correct answer Kukurykus

In both alerts use at end:

.toSource()

4 replies

Stephen Marsh
Community Expert
Community Expert
October 2, 2021

@rotoworld 

 

So, where are you at?

rotoworldAuthor
Participant
October 3, 2021

Thanks for the help guys, indeed the problem was I was expecting it to work like a string!

Stephen Marsh
Community Expert
Community Expert
October 3, 2021

I am in uncharted territory, the following summary is my take from the discussion and minor research around the edges.

 

I believe that if the data source is a separate external JSON file, then it can be parsed and treated as a standard JS object, where you can easily retrieve the properties as one would, similar to a standard DOM object.

 

If the JSON data is "inline" in the JS code, it could be handled as a string with JS text string/regular expressions (not an object).

 

I'm not sure if it is possible to convert a variable with a string of JSON "code" into a JS object as is possible with an external file.

 

 

Kukurykus
KukurykusCorrect answer
Legend
September 30, 2021

In both alerts use at end:

.toSource()
Stephen Marsh
Community Expert
Community Expert
September 30, 2021

Thanks I knew of this in different contexts... So:

 

var layLen = app.activeDocument.layers.length;
alert(layLen);

 

Returns:
2

 

While:

 

var layLen = app.activeDocument.layers.length;
alert(layLen.toSource());

 

Returns:

(new Number(2))

Legend
September 30, 2021

Yes, "toSource()" and "toString()" and "eval" are your friends. You can also dump data into a text file and inspect that, which has the added advantage of giving you a de facto log file.

Legend
September 30, 2021

Read this, which quotes the manual Script Alert - Title - Alert Box

 

alert is NOT a data formatter or a data dumper. It simply displays a string. So your non-string object is converted by normal JavaScript rules to a string object like "[object Object]".

Stephen Marsh
Community Expert
Community Expert
September 30, 2021

Oh no, I'm guilty of that... data dumping to "inspect"!

 

Using Visual Studio Code ExtendScript Debugger, what is the alternative to using alert, something for the terminal/output tab?

 

var layLen = app.activeDocument.layers.length;
alert(layLen);

 

Obviously console.log(layLen); works in a browser, but not in Photoshop...

 

 

JJMack
Community Expert
Community Expert
September 30, 2021

Where is the script code we have no idea  what the object in your alert messages are or how you are trying to use them. The data array seem to be missing the ; after the ]. The 

alert

(uppgifter).

alert

(data[

0

]);

 

look strange  and uppgifter is unknown to me.

JJMack