-2

I have tried jsonata exerciser. It looks cool.

But I have to implement it in my code(NodeJS).

Let's say

Input is:

{
  "id": "course_uuid1",
  "description": "Sample course description",
  "contentType": "COURSE",
  "category": "Course",
  "durationInSeconds": 500,
  "expertiseLevels": ["INTERMEDIATE"],
  "imageUrl": "https://percipio.com/courseuuid1/imagelink",
  "link": "https://percipio.com/courseuuid1",
  "modalities": ["LISTEN", "READ", "WATCH"],
  "languageCode": "en",
  "parent": null,
  "publishDate": "2018-11-19T10:23:34Z",
  "sourceName": null,
  "technologyTitle": null,
  "technologyVersion": null,
  "title": "Java",
  "by": ["admin"]
}

Transformation logic is:

{
  "pkID": id,
  "description": description,
  "componentTypeID": contentType,
  "totalLength": durationInSeconds,
  "thumbnailURI": imageUrl,
  "locale": languageCode,
  "createTimestamp": publishDate,
  "title": title,
  "lastUpdateUser": by
}

I want output in the transformation logic format, but through NODEJS code.

Please suggest

Nimmo
  • 105
  • 10

2 Answers2

2

Install jsonata node module and try below code:

var jsonata = require('jsonata');
    let input=
    {
      "id": "course_uuid1",
      "description": "Sample course description",
      "contentType": "COURSE",
      "category": "Course",
      "durationInSeconds": 500,
      "expertiseLevels": ["INTERMEDIATE"],
      "imageUrl": "https://percipio.com/courseuuid1/imagelink",
      "link": "https://percipio.com/courseuuid1",
      "modalities": ["LISTEN", "READ", "WATCH"],
      "languageCode": "en",
      "parent": null,
      "publishDate": "2018-11-19T10:23:34Z",
      "sourceName": null,
      "technologyTitle": null,
      "technologyVersion": null,
      "title": "Java",
      "by": ["admin"]
    }
    let exp="{'pkID': id,'description': description,'componentTypeID': contentType,'totalLength': durationInSeconds,'thumbnailURI': imageUrl,'locale': languageCode,'createTimestamp': publishDate,'title': title,'lastUpdateUser': by}";
    let expression = jsonata(exp);
    let expResult = expression.evaluate(input);
    console.log(expResult);
eagle
  • 312
  • 1
  • 13
  • One more thing, i want to do transform publishDate(input) in unix time stamp.How can i achieve the same through this code. – Nimmo Nov 29 '18 at 10:08
  • @Nimmo you can convert the date and use that in the expression. below link may help you to convert date to UNIX timestamp https://stackoverflow.com/questions/1791895/converting-date-and-time-to-unix-timestamp – eagle Nov 29 '18 at 10:21
1

I think this is what you want:

var inputJson={
  "id": "course_uuid1",
  "description": "Sample course description",
  "contentType": "COURSE",
  "category": "Course",
  "durationInSeconds": 500,
  "expertiseLevels": ["INTERMEDIATE"],
  "imageUrl": "https://percipio.com/courseuuid1/imagelink",
  "link": "https://percipio.com/courseuuid1",
  "modalities": ["LISTEN", "READ", "WATCH"],
  "languageCode": "en",
  "parent": null,
  "publishDate": "2018-11-19T10:23:34Z",
  "sourceName": null,
  "technologyTitle": null,
  "technologyVersion": null,
  "title": "Java",
  "by": ["admin"]
};

var outputJson={
  "pkID": inputJson.id,
  "description": inputJson.description,
  "componentTypeID": inputJson.contentType,
  "totalLength": inputJson.durationInSeconds,
  "thumbnailURI": inputJson.imageUrl,
  "locale": inputJson.languageCode,
  "createTimestamp": inputJson.publishDate,
  "title": inputJson.title,
  "lastUpdateUser": inputJson.by
}
Jeremy Su
  • 781
  • 1
  • 6
  • 12