Skip to main content
Known Participant
December 22, 2021
Answered

read json file in the desktop using jquery

  • December 22, 2021
  • 1 reply
  • 3475 views

Hi All,

Im working in CEP panel, my requirement is to read json file in the desktop and insert the data into html fields.

 

I can't able to read the json file in the local path using jquery. And also I tried to integrate the JS code in Jquery that also not works.

Is there any option to fetch the data in the local path using jquery. Any help is much appreciated.

Thanks

This topic has been closed for replies.
Correct answer Charu Rajput

Sure I will send! Thanks


I have seen your code. There are two issues

1. JSON file data

2. Use complete path to the file in the _filePath variable

 

I hope it will work after doing these changes.

1 reply

Charu Rajput
Community Expert
December 22, 2021

Hi,

In CEP panels you can use node module 'fs' to read the file. Below is the simple example to read json file in CEP panel.

 

var filePath = '/Users/charu/Desktop/test.json'
_jsonData = cep.fs.readFile(filePath)
if (_jsonData.err == 0) {
    var _data = JSON.parse(_jsonData.data)
}

 

 

NOTE: Change filePath value as per your requirements.

Best regards
Rocky@Author
Known Participant
December 23, 2021

Thanks Charu! 

This Code was not work at my end. If I need to include any library js file for this code works?

 

Rocky@Author
Known Participant
December 23, 2021

There is no need to include anything. These nodes modules are in built. When you say it is not working, could you please explain what exactly not working or what error are you getting? You just need to enable the node parameter in manifest file.


I have customized the XMP Sample panel and created new fields. Actually Im trying to update the values for fields from the json file. I tried many ways but won't work and integrated your code in the below app.js file but it doesn't work. Yes enabled the node parameter in manifest file. 

 

var filePath = '~/Desktop/RelayMetadata.json'
_jsonData = cep.fs.readFile(filePath)

//if (_jsonData.err == 0) {
	var _data = JSON.parse(_jsonData.data)
	alert(_data)
//}


$(document).ready(function () {

	/**
	 * Helper function that extracts all mandatory information needed to reference an XMP property 
	 * from a given HTML node and constructs a simple data object based on that.
	 */
	function getFieldInfo(elem, callback) {
		var $elem = $(elem);

		var field = {
			elem: $elem,
			property: $elem.attr('data-property'),
			namespace: $elem.attr('data-namespace'),
			value: $elem.val()
		};

		if ($elem.attr('data-namespace-ref')) {

			// if namespace-ref is present it needs to be resolved to a valid URI via XMPBridge.
			XMPBridge.toNamespaceURI($elem.attr('data-namespace-ref'), function (namespaceURI) {
				field.namespace = namespaceURI;
				callback(field);
			});

		} else {
			callback(field);
		}
	}


	/**
	 * We're registering our data binding code for execution once the XMPBridge is ready to use.
	 * This prevents us to access any property before the ExtendScript library is initialized 
	 * properly.
	 */
	XMPBridge.onInit(function (state) {

		if (!state.isError) {

			$("#main").show();

			// retrieve a descriptive name for the active target item�(e.g. active document, footage, ...)
			XMPBridge.getTargetName(function (targetName) {

				// stored in an arbitrary HTML element with id="target-name"
				$('#target-name').html(targetName);

			});

			// all html nodes having a "data-property" attribute will be considered for data binding.
			var $fields = $("[data-property]");

			$fields.each(function () {
				getFieldInfo(this, function (field) {

					XMPBridge.read(field.namespace, field.property, function (value) {

						// form fields are initialized with the property's current value ...
						if (field.elem.is('input,textarea')) {
							if(field.elem.attr('data-property') == 'hww_id_deliverable'){
								field.elem.val("XXXX");
							}
							if(field.elem.attr('data-property') == 'hww_product'){
								field.elem.val("test1");
							}
				
							//	field.elem.val(value);
							// ... for other elements we just replace the node's inner HTML.
						} else {
							field.elem.html(value);
						}

					});

				});
			});

			// if the form is being submitted, we update the metadata and write it back into the application DOM.
			$("form").submit(function () {

				// only process writeable property fields.
				$fields.filter('input,textarea').each(function () {
					getFieldInfo(this, function (field) {
						XMPBridge.write(field.namespace, field.property, field.value);
					});
				});

				// commit the changes so they're reflected by the application DOM.
				XMPBridge.commit();

			});

		} else {

			$("#message .text").html(state.statusMessage).parent().fadeIn();

		}

	});

});