Skip to main content
AG_Ps_100
Inspiring
December 2, 2021
Question

Photoshop Script that reads a JSON file

  • December 2, 2021
  • 2 replies
  • 4368 views

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!

This topic has been closed for replies.

2 replies

Kukurykus
Legend
December 2, 2021
json = """[
{"SKU":"AAA","TITLE":"Banana"},
{"SKU":"BBB","TITLE":"Peach"}
]"""

arr = json.match(/\w+(?="(,|}))/g)
while(arr.length) alert(arr.shift())
AG_Ps_100
AG_Ps_100Author
Inspiring
December 6, 2021

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?

Kukurykus
Legend
December 6, 2021

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())

 

Legend
December 2, 2021

 

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]) } }
}

 

AG_Ps_100
AG_Ps_100Author
Inspiring
December 6, 2021

Thank you!

it worked.

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