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

How to read a local Json file using CEP

New Here ,
Sep 17, 2021 Sep 17, 2021
my json content : { "name": "JA"}
 

 

var path = "my Json path";
var result = window.cep.fs.readFile(path);
 
cs.evalScript('alert(' + result.data + ')');  // object
cs.evalScript('alert(' + result.data.name + ')');   // undefined
cs.evalScript('alert(' + JSON.stringify(result.data)+  ')');  // { "name": "JA"}
cs.evalScript('alert(' + JSON.stringify(result.data).name+  ')');  // undefined

 

How can i get the "name" info : JA.

Thanks for your help !

TOPICS
Scripting , SDK
462
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

Community Expert , Sep 20, 2021 Sep 20, 2021
JSON.stringify(result.data)

produces a string and hence

JSON.stringify(result.data).name

tries to access a property "name" of that string, which does not exist.

You probably want this (where the stringify effectively puts quotes around the name and adds any necessary escapes)

cs.evalScript('alert(' + JSON.stringify(result.data.name)+  ')');
Translate
Community Expert , Sep 22, 2021 Sep 22, 2021

You want to parse the data, not stringify. It's already a string when you read it.

 

const cs = new CSInterface();
const dataStr = cep.fs.readFile('C:/path/to/file.json').data;
const dataObj = JSON.parse(dataStr);
const name = dataObj.name;
cs.evalScript('alert("' + name + '")');

 

Translate
Community Expert ,
Sep 20, 2021 Sep 20, 2021
JSON.stringify(result.data)

produces a string and hence

JSON.stringify(result.data).name

tries to access a property "name" of that string, which does not exist.

You probably want this (where the stringify effectively puts quotes around the name and adds any necessary escapes)

cs.evalScript('alert(' + JSON.stringify(result.data.name)+  ')');
Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects
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 22, 2021 Sep 22, 2021
LATEST

You want to parse the data, not stringify. It's already a string when you read it.

 

const cs = new CSInterface();
const dataStr = cep.fs.readFile('C:/path/to/file.json').data;
const dataObj = JSON.parse(dataStr);
const name = dataObj.name;
cs.evalScript('alert("' + name + '")');

 

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