Search code examples
javascriptjsonjelastic

How do I manipulate a json file from a script run on a jelastic jps manifest?


I have a JSON file available at some URL. In my Jelastic manifest, I want to read it, modify its content, and output that modified content to a json on the environment I am installing / updating:

jpsVersion: 1.3
jpsType: update
application:
  id: json
  name: JSON Test
  version: 0.0

  onInstall:
  - script: |
      const Transport = com.hivext.api.core.utils.Transport
      const body = new Transport().get('url-to-my-jsonfile.json')
      // 1. here I want to edit the json data, maybe change some values
      // 2. here I want to store the json data in a json file

In the above manifest, I first download the json file from some remote endpoint (most probably a raw file from some git repository). Then, I want to load the json into a javascript object, in order to manipulate that object. For example, I'd like to change the value of one of its keys. Finally, I want to output the modified json object somewhere in my environment, e.g. in the /data/my-output.json file.

How do I do that? I tried some stuff, but nothing seems to be working. For example, it seems like JSON.parse is not available in such scripts. What options do I have?

EDIT

Currently, what I am doing is that I use a python script that I wrote to manipulate those data. I am wondering if there is something easier with the Jelastic API or available from the javascript scripts.


Solution

  • You can use pre-defined function toNative which converts json text to java objects:

    body = toNative(body)

    There is an example.

    And it's possible to save data as a file in a container via API jelastic.environment.file.Write

    var resp = jelastic.environment.file.Write({
        envName: 'myenv',
        session: session,
        nodeid: 12345,
        path: '/home/jelastic/myfile.json',
        body: '{a:1}'
    })