-1

I trying to write to the jsong file. I want my json file should be written with indentation. But It has only one line. It is possible in python. How to do it in ballerina.

[
{
    "employeeId": 2413,
    "odometerReading": 4089,
    "gallons": 21.682,
    "gasPrice": 3.46
},
{
    "employeeId": 3423,
    "odometerReading": 6582,
    "gallons": 15.248,
    "gasPrice": 4.56
},
{
    "employeeId": 2413,
    "odometerReading": 4127,
    "gallons": 4.221,
    "gasPrice": 3.40
},
{
    "employeeId": 2413,
    "odometerReading": 4349,
    "gallons": 11.192,
    "gasPrice": 4.10
},
{
    "employeeId": 3423,
    "odometerReading": 6767,
    "gallons": 8.696,
    "gasPrice": 3.34
},
{
    "employeeId": 2413,
    "odometerReading": 4547,
    "gallons": 9.197,
    "gasPrice": 2.90
}

]

But My file has

[{"employeeId":2413, "gasFillUpCount":4, "totalFuelCost":161.92962, "totalGallons":46.292, "totalMilesAccured":458}, {"employeeId":3423, "gasFillUpCount":2, "totalFuelCost":98.57552, "totalGallons":23.944, "totalMilesAccured":185}]

2 Answers2

1

Adding to the @ThisaruG answer, if you want the indentation(without the escape characters) in the JSON file, you can use the io:fileWriteString instead of io:fileWriteJson. Please refer to the following example,

import ballerina/io;
import thisarug/prettify;

public function main() returns error? {
    // Initializes the JSON file path and content.
    string jsonFilePath = "./files/jsonFile.json";

    json jsonContent = [{"employeeId": 2413, "odometerReading": 4089, "gallons": 21.682, "gasPrice": 3.46}];

    string prettified = prettify:prettify(jsonContent);

    check io:fileWriteString(jsonFilePath, prettified);
}
sanoJ
  • 2,935
  • 2
  • 8
  • 18
  • Thanks This is working, One little help Can use this package in the ballerina.tomal file? How to do it for auto pulling. If I want to run my programme in another system. – Vishal Vikal Sep 23 '22 at 10:29
0

You can use the prettify library in Ballerina for this. It is a third party library though.

import thisarug/prettify;

public function main() {
    json value = { name: “Sam” };
    string prettified = prettify:prettify(value);

}

You can check out the documentation here: https://central.ballerina.io/thisarug/prettify

ThisaruG
  • 3,222
  • 7
  • 38
  • 60
  • Its showing error while imorting it. cannot resolve module thisarug – Vishal Vikal Sep 22 '22 at 18:33
  • You have to pull it from the central. VACode plugin should provide an easy fix, or you can manually pull it using `bal pull thisarug/prettify` – ThisaruG Sep 22 '22 at 18:45
  • Actually I am trying to write to the file. I am not trying to display it. How to do it. It is writing a string with escape characters. in the json file. I want to upload as a json. I am using fileWriteJson. – Vishal Vikal Sep 23 '22 at 01:45
  • Your original question was “how to indent json”. Weiting a string to a file can be done using the ballerina/io library. Indenting a JSON can be done using the prettify library. Those are two different things. – ThisaruG Sep 23 '22 at 12:37