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

Photoshop Script that reads a JSON file

Engaged ,
Dec 02, 2021 Dec 02, 2021

Copy link to clipboard

Copied

Hello everyone,

I'm trying to read a JSON file using a photoshop script.

 

I will select the JSON file via File Dialog:

var selectedFile = app.openDialog();

 

I want to loop through all JSON's objects and alert the values. (this is just a test)

for that here is an example of a JSON I made:

[
{"SKU":"AAA","TITLE":"Banana"},
{"SKU":"BBB","TITLE":"Peach"}
]

 

To sum up:

The photoshop script will pop a dialog asking the user to select a JSON file, and then it will show 4 alerts: AAA, Banana, BBB, Peach

 

 

Any help will be much appreciated!

TOPICS
Actions and scripting , Windows

Views

1.6K

Translate

Translate

Report

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
Adobe
Guide ,
Dec 02, 2021 Dec 02, 2021

Copy link to clipboard

Copied

 

var f = File.openDialog('Select file', '*.json', false)
if (f) {
  var s = '('
  f.open('r')
  while (!f.eof) { s += f.readln() }
  f.close()
  var o = eval(s + ')')
  for (var i = 0; i < o.length; i++) { for (var a in o[i]) { alert(o[i][a]) } }
}

 

Votes

Translate

Translate

Report

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
Engaged ,
Dec 06, 2021 Dec 06, 2021

Copy link to clipboard

Copied

Thank you!

it worked.

if I had a CSV instead of a JSON file, what would you suggest me to do then?

Votes

Translate

Translate

Report

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 ,
Dec 02, 2021 Dec 02, 2021

Copy link to clipboard

Copied

json = """[
{"SKU":"AAA","TITLE":"Banana"},
{"SKU":"BBB","TITLE":"Peach"}
]"""

arr = json.match(/\w+(?="(,|}))/g)
while(arr.length) alert(arr.shift())

Votes

Translate

Translate

Report

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
Engaged ,
Dec 06, 2021 Dec 06, 2021

Copy link to clipboard

Copied

thank you Kukurykus, I like this solution of making an array out of the JSON.

I have 2 questions

 

1. how can I know when an object ends? for example to alert "object 1 SKU: AAA"

2. if I had a CSV instead of a JSON file, what would you suggest me to do then?

Votes

Translate

Translate

Report

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 ,
Dec 06, 2021 Dec 06, 2021

Copy link to clipboard

Copied

1) check its length:

 

({a: 1, b: 2, c: 3, d: 4}).__count__

 

2) use following code:

 

with(File('~/desktop/TEST.csv'))
	open('r'), cntnt = read(), close()
arr = cntnt.slice(10).split(/,|\n/g)
while(arr.length) alert(arr.shift())

 

Votes

Translate

Translate

Report

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
Engaged ,
Dec 07, 2021 Dec 07, 2021

Copy link to clipboard

Copied

Check out this CSV, it doesn't work my friend.

I think the "slice(10)" is not a general solution, I wanted to ask you about a general solution that if I want to have different number of rows/columns it will stil work.

 

I'm basically need to know when a row ends (meaning each row is a product)

Votes

Translate

Translate

Report

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 ,
Dec 07, 2021 Dec 07, 2021

Copy link to clipboard

Copied

Change:

slice(10)

 

to:

split(/^.*\n/)[1]

 

Votes

Translate

Translate

Report

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
Engaged ,
Dec 07, 2021 Dec 07, 2021

Copy link to clipboard

Copied

works now 🙂

I still need to know when a row ends because I need to make different code for each row, so my question is how for example will I alert:

"This is row 2" (and then after it "AAA", "Apple", "5")

 

I'm still figuring out what these do

split(/,|\n/g)
split(/^.*\n/)[1]

 

Votes

Translate

Translate

Report

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 ,
Dec 08, 2021 Dec 08, 2021

Copy link to clipboard

Copied

Change last 2 lines to:

 

alert('This is: ' + cntnt.split(/\n/g)[1])

\n is end of line, | is or, ^ is beginning, .* is everything in line, g is in entire file

 

Votes

Translate

Translate

Report

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
Engaged ,
Dec 13, 2021 Dec 13, 2021

Copy link to clipboard

Copied

Thank you for the clear explaination 🙂

I got the CSV to work, if I wanted to make an array of the titles called CSV_Titles (which will be an array of 2 cells "TITLE,SKU")

 

what will I need to write inside the split please?

 

Votes

Translate

Translate

Report

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 ,
Dec 13, 2021 Dec 13, 2021

Copy link to clipboard

Copied

I can't imagine this .csv without seeing it to understand what you exactly mean this time.

Votes

Translate

Translate

Report

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
Engaged ,
Dec 13, 2021 Dec 13, 2021

Copy link to clipboard

Copied

I managed to make it work for now anyway 🙂

Thanks for the professionalism and kindness.

Votes

Translate

Translate

Report

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 ,
Dec 13, 2021 Dec 13, 2021

Copy link to clipboard

Copied

LATEST

Could you explain though what you needed?

Votes

Translate

Translate

Report

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