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!
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]) } }
}
Copy link to clipboard
Copied
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())
Copy link to clipboard
Copied
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())
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Change:
slice(10)
to:
split(/^.*\n/)[1]
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]
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
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?
Copy link to clipboard
Copied
I can't imagine this .csv without seeing it to understand what you exactly mean this time.
Copy link to clipboard
Copied
I managed to make it work for now anyway 🙂
Thanks for the professionalism and kindness.
Copy link to clipboard
Copied
Could you explain though what you needed?
Find more inspiration, events, and resources on the new Adobe Community
Explore Now