-1

I have a post with Custom Field called json_data, inside there is an array of strings such as: {"data":["string no 1","string no 2","string no 3"]}.

Is there a way to read the data using JavaScript and Elementor without editing the PHP file itself?

1 Answer 1

1

Yes. Use JSON.parse($string); to convert your string value to JSON format within Javascript.

<script>
    var json_data_string = '{"data":["string no 1","string no 2","string no 3"]}'; // or echo the json_data field as <?php echo $json_data; ?>
    var json_data = JSON.parse(json_data_string);
    // Now access the properties as 
    $data = json_data.data; // outputs: string no 1,string no 2,string no 3
    $string1 = json_data.data[0]; // outputs: string no 1 
</script>

Reference to JSON.parse() function in Javascript.

2
  • Amazing, thanks for the answer. But how I load the json_data from the Custom Field?
    – ytpm
    Nov 29, 2022 at 12:11
  • Where do you want to load the field data? In the backend or frontend? If backend, where exactly? Like in another metabox?
    – Aqsa J.
    Nov 29, 2022 at 12:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.