Convert Pandas DataFrame to JSON format

The output that you get after DF.to_json is a string. So, you can simply slice it according to your requirement and remove the commas from it too. out = df.to_json(orient=”records”)[1:-1].replace(‘},{‘, ‘} {‘) To write the output to a text file, you could do: with open(‘file_name.txt’, ‘w’) as f: f.write(out)

How to serialize or convert Swift objects to JSON?

In Swift 4, you can inherit from the Codable type. struct Dog: Codable { var name: String var owner: String } // Encode let dog = Dog(name: “Rex”, owner: “Etgar”) let jsonEncoder = JSONEncoder() let jsonData = try jsonEncoder.encode(dog) let json = String(data: jsonData, encoding: String.Encoding.utf16) // Decode let jsonDecoder = JSONDecoder() let secondDog = … Read more

Standardized way to serialize JSON to query string?

URL-encode (https://en.wikipedia.org/wiki/Percent-encoding) your JSON text and put it into a single query string parameter. for example, if you want to pass {“val”: 1}: mysite.com/path?json=%7B%22val%22%3A%201%7D Note that if your JSON gets too long then you will run into a URL length limitation problem. In which case I would use POST with a body (yes, I know, … Read more

Modify a key-value in a json using jq in-place

Use a temporary file; it’s what any program that claims to do in-place editing is doing. tmp=$(mktemp) jq ‘.address = “abcde”‘ test.json > “$tmp” && mv “$tmp” test.json If the address isn’t hard-coded, pass the correct address via a jq argument: address=abcde jq –arg a “$address” ‘.address = $a’ test.json > “$tmp” && mv “$tmp” … Read more

express.json vs bodyParser.json

Earlier versions of Express used to have a lot of middleware bundled with it. bodyParser was one of the middleware that came with it. When Express 4.0 was released they decided to remove the bundled middleware from Express and make them separate packages instead. The syntax then changed from app.use(express.json()) to app.use(bodyParser.json()) after installing the … Read more

PostgreSQL return result set as JSON array?

TL;DR SELECT json_agg(t) FROM t for a JSON array of objects, and SELECT json_build_object( ‘a’, json_agg(t.a), ‘b’, json_agg(t.b) ) FROM t for a JSON object of arrays. List of objects This section describes how to generate a JSON array of objects, with each row being converted to a single object. The result looks like this: … Read more