Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Scripting for photoshop - JSON just returns [object Object]

Community Beginner ,
Sep 30, 2021 Sep 30, 2021

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?
TOPICS
Actions and scripting , Windows
2.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

LEGEND , Sep 30, 2021 Sep 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]".

Translate
LEGEND , Sep 30, 2021 Sep 30, 2021

In both alerts use at end:

.toSource()
Translate
Adobe
Community Expert ,
Sep 30, 2021 Sep 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 30, 2021 Sep 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]".

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 30, 2021 Sep 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...

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 30, 2021 Sep 30, 2021

In both alerts use at end:

.toSource()
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 30, 2021 Sep 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))

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 30, 2021 Sep 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 30, 2021 Sep 30, 2021

You mean 'uneval' as alternative for 'toSource()', but surely not '.toString()' 😉

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 30, 2021 Sep 30, 2021

If you are using alerts, the input is a string. Extendscript usually converts it ok but sometimes it fails miserably. Of course there is no way to tell when its going to work. LOL

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 01, 2021 Oct 01, 2021

@rotoworld 

 

So, where are you at?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 02, 2021 Oct 02, 2021

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 03, 2021 Oct 03, 2021
LATEST

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.

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines