0

I'm trying to replace the time value in a JSON file with current time using ROBOT framework, however the time format is in "YYYY-MM-DDTHH:MM:SS.SZ" and I'm not able to figure that out...

x.json

{

"abc": "123",

"xyz": [ "a1b2c3"],

"time": "2021-02-04T13:01:27.4Z"

}

I want to update it with current time or [current time + 3-5 mins], Let's say current time is 18:00 I need the value in JSON to be 18:05

updated x.json

{

"abc": "123",

"xyz": [ "a1b2c3"],

"time": "2021-02-04T18:05:00.0Z"

}

I tried doing with sed but it cant change into "YYYY-MM-DDTHH:MM:SS.SZ" format. Even date -u doesn't return in the above format.

Is there a way of doing this using ROBOT + linux commands?

Uday T
  • 127
  • 7
  • What does this have to do with Python or even programming? As for the format, it's nothing strange, unusual or special. It's the ISO8601 format for date *literals* (ie strings representing a date), used by most languages, databases, JavaScript itself and the defacto date standard for JSON. *All* JSON parsers and serializers recognize it and emit dates in that format. You can deserialize the original JSON text, modify the objects you want then serialize them back to the file – Panagiotis Kanavos Feb 04 '21 at 17:45
  • Does this answer your question? [Extract json value with sed](https://stackoverflow.com/questions/55607925/extract-json-value-with-sed) – Panagiotis Kanavos Feb 04 '21 at 17:48
  • Is it important that the updated file have the exact same format (multiple lines, with blank lines), or only that it is valid JSON? – Bryan Oakley Feb 04 '21 at 22:13

1 Answers1

0

Robot has a DateTime library which makes it easy to convert strings to dates, and to get the current date. Once you have a date object you can manipulate it however you want.

The following example will do the following:

  • import the json data from x.json
  • convert it to a python object
  • get the current date
  • save the new date to the python object
  • convert the python object back to a json string
  • write the json string to x.json

This doesn't preserve the exact format of the original data (ie: blank lines are not preserved), but x.json will still be valid json but with the time changed.

*** Settings ***
Library  DateTime
Library  OperatingSystem
Library  Collections

*** Test cases ***
Example 1
    # read the raw data
    ${json}=   Get file   x.json

    # convert the data to a python object
    ${object}=  Evaluate  json.loads($json)

    # Get the current date/time in UTC
    ${new_date}=  get current date  time_zone=utc

    # save the new data to the dictionary
    Set to dictionary  ${object}  time=${new_date}

    # convert the object back to json
    ${json}=  evaluate  json.dumps($object, indent=4)

    log to console  \n${json}

    # overwrite the original file with the new json
    create file  x.json  content=${json}
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Thanks for this but I don't want to hardcode the time value, it has to be replaced with present/current time. – Uday T Feb 05 '21 at 04:28
  • 1
    @UdayT: this was just an example. If you want the current time, the DateTime library has a function that will return it, and then you can use the current time instead of the hard-coded time. Do you want the current date, too? That would be even easier. – Bryan Oakley Feb 05 '21 at 04:32
  • Yes @Bryan, exactly what I'm looking for, date and time replaced with present date and time. – Uday T Feb 05 '21 at 05:22
  • you mean this: "Get Current Date UTC" ? – Uday T Feb 05 '21 at 05:26