There are 94 Unicode characters which can be represented as one byte according to the JSON spec (if your JSON is transmitted as UTF-8). With that in mind, I think the best you can do space-wise is base85 which represents four bytes as five characters. However, this is only a 7% improvement over base64, it's more expensive to compute, and implementations are less common than for base64 so it's probably not a win.

You could also simply map every input byte to the corresponding character in U+0000-U+00FF, then do the minimum encoding required by the JSON standard to pass those characters; the advantage here is that the required decoding is nil beyond builtin functions, but the space efficiency is bad -- a 105% expansion (if all input bytes are equally likely) vs. 25% for base85 or 33% for base64.

Final verdict: base64 wins, in my opinion, on the grounds that it's common, easy, and not bad enough to warrant replacement.

See also: Base91 and Base122

Answer from hobbs on Stack Overflow
🌐
Reddit
reddit.com › r/azure › how to convert binary file into json?
r/AZURE on Reddit: How to convert binary file into json?
November 21, 2022 -

I want to get a json file from a binary file to make it more readable and usable further. However, there's a limitation of adf that binary files must have the source and sink as binary when using copy activity.

Is there any other activity or azure service that i can leverage to achieve the same. Has anyone ever worked on this problem statement before?

Discussions

How to convert JavaScript array to binary data and back for WebSocket? - Stack Overflow
Convert the array into binary data, and send the binary to my WebSocket server. More on stackoverflow.com
🌐 stackoverflow.com
javascript - binary encoding for JSON? - Stack Overflow
My Javascript application is downloading quite a bit of data from the server and I was thinking that in addition to normal gzip that's done by the server, I can encode the data in some binary format rather than textual JSON. Is there a standard way of doing this? Ideally it should be some small tool that can take a JSON text file and convert ... More on stackoverflow.com
🌐 stackoverflow.com
Convert Binary file To JSON or CSV
Describe the issue/error/question Hello everyone, I have to transform a file in Binary (more than 400mb) downloaded from sFTP server and then, use that data in a CSV or JSON format. Do you know the best way to handle a file like that and convert it in CSV or JSON? More on community.n8n.io
🌐 community.n8n.io
1
1
March 28, 2023
Javascript from Buffer to JSON - Stack Overflow
I'm using bleno (A node js BLE package) and it uses Buffer to send and receive data. How will I go about getting a Buffer object and converting it into JSON? This is what i have now: bufferToJson = More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 16
593

There are 94 Unicode characters which can be represented as one byte according to the JSON spec (if your JSON is transmitted as UTF-8). With that in mind, I think the best you can do space-wise is base85 which represents four bytes as five characters. However, this is only a 7% improvement over base64, it's more expensive to compute, and implementations are less common than for base64 so it's probably not a win.

You could also simply map every input byte to the corresponding character in U+0000-U+00FF, then do the minimum encoding required by the JSON standard to pass those characters; the advantage here is that the required decoding is nil beyond builtin functions, but the space efficiency is bad -- a 105% expansion (if all input bytes are equally likely) vs. 25% for base85 or 33% for base64.

Final verdict: base64 wins, in my opinion, on the grounds that it's common, easy, and not bad enough to warrant replacement.

See also: Base91 and Base122

2 of 16
340

I ran into the same problem, and thought I'd share a solution: multipart/form-data.

By sending a multipart form you send first as string your JSON meta-data, and then separately send as raw binary (image(s), wavs, etc) indexed by the Content-Disposition name.

Here's a nice tutorial on how to do this in obj-c, and here is a blog article that explains how to partition the string data with the form boundary, and separate it from the binary data.

The only change you really need to do is on the server side; you will have to capture your meta-data which should reference the POST'ed binary data appropriately (by using a Content-Disposition boundary).

Granted it requires additional work on the server side, but if you are sending many images or large images, this is worth it. Combine this with gzip compression if you want.

IMHO sending base64 encoded data is a hack; the RFC multipart/form-data was created for issues such as this: sending binary data in combination with text or meta-data.

🌐
GitHub
github.com › dcodeIO › PSON
GitHub - dcodeIO/PSON: A super efficient binary serialization format for JSON.
Additionally it comes with progressive and static dictionaries to reduce data redundancy to a minimum. In a nutshell: ... This repository contains a plain node.js/CommonJS, RequireJS/AMD and Browser compatible JavaScript implementation of the PSON specification on top of ByteBuffer.js and optionally Long.js:
Starred by 462 users
Forked by 32 users
Languages   JavaScript 85.1% | CSS 14.9% | JavaScript 85.1% | CSS 14.9%
🌐
npm
npmjs.com › package › js-binary
js-binary - npm
November 27, 2017 - This module is analogous to JSON.stringify and JSON.parse, but instead of a JSON string, the data is encoded to a custom binary format (using a Buffer instance to store the data).
      » npm install js-binary
    
Published   Nov 27, 2017
Version   1.2.0
Author   Sitegui
🌐
Quora
quora.com › How-do-you-send-binary-data-in-JSON
How to send binary data in JSON - Quora
Each number could represent one byte, two bytes, or four bytes (since JavaScript's max safe integer is larger than 32 bits). Read it into a UInt8Array, UInt16Array, or UInt32Array then convert it from there.
Top answer
1 of 2
17

First of all, Browsers treat binary data differently than NodeJS. In browser, binaries can be seen as Blob or ArrayBuffer, but in NodeJS, it is seen as Buffer doesn't understand ArrayBuffer. I won't go in too deep into this, but you need to handle data differently between browser and nodeJS.

When using WebSocket at the browser side, data are transmitted as either string or binary, if binary will be used, then you have to specify BinaryType, and in this particular case, I will use ArrayBuffer.

As to string to buffer, I suggest to use the standard UTF-8 as there are 2 ways of encoding UTF-16. For example '\u0024' in UTF-16 will be stored as 00 24 in UTF-16BE, and in UTF-16LE, it is stored as 24 00. That is, if you are going to use UTF-16, then you should use TextEncoder and TextDecoder. Otherwise you can simply do this

strToAB = str =>
  new Uint8Array(str.split('')
    .map(c => c.charCodeAt(0))).buffer;

ABToStr = ab => 
  new Uint8Array(ab).reduce((p, c) =>
  p + String.fromCharCode(c), '');

console.log(ABToStr(strToAB('hello world!')));

For UTF-16, the browser code should be something like:

const ENCODING = 'utf-16le';
var ws = new WebSocket('ws://localhost');

ws.binaryType = 'arraybuffer';
ws.onmessage = event => {
  let str = new TextDecoder(ENCODING).decode(event.data),
    json = JSON.parse(str);
    console.log('received', json);
};
ws.onopen = () => {
  let json = { client: 'hi server' },
    str = JSON.stringify(json);
  console.log('sent',json);

  //JSON.toString() returns "[object Object]" which isn't what you want,
  //so ws.send(json) will send wrong data.
  ws.send(new TextEncoder(ENCODING).encode(str));
}

At the server side, data is stored as Buffer and it more or less does everything natively. You however need to specify Encoding unless it is UTF-8.

const ENCODING = 'utf-16le';
//You may use a different websocket implementation, but the core
//logic reminds as they all build on top of Buffer.
var WebSocketServer = require('websocket').server,
  http = require('http'),
  //This is only here so webSocketServer can be initialize.
  wss = new WebSocketServer({
    httpServer: http.createServer()
      .listen({ port: 80 })});

wss.on('request', request => {
  var connection = request.accept(null, request.origin);
  connection.on('message', msg => {
    if (msg.type === 'binary') {
      //In NodeJS (Buffer), you can use toString(encoding) to get
      //the string representation of the buffer.
      let str = msg.binaryData.toString(ENCODING);
      console.log(`message : ${str}`);

      //send data back to browser.
      let json = JSON.parse(str);
      json.server = 'Go away!';
      str = JSON.stringify(json);

      //In NodeJS (Buffer), you can create a new Buffer with a
      //string+encoding, and the default encoding is UTF-8.
      let buf = new Buffer(str, ENCODING);
      connection.sendBytes(buf);
    }
  });
});
2 of 2
2

Try it:

Sending data example:

var data = [{
    id: 1,
    name: "test",
    position: [1234, 850], //random position on the map
    points: 100 //example points
}];

var data2 = new Uint16Array(data);
socket.send(data2);

In your event onMessage websocket try it:

function onMessage(event) {

    if (event.data instanceof window["ArrayBuffer"]){
        var data3 = JSON.parse(String.fromCharCode.apply(null, new Uint16Array(event.data)));
    };

};
🌐
npm
npmjs.com › package › typed-binary-json
typed-binary-json - npm
December 3, 2024 - It stores known object prototypes in a JSON header, and serializes the data in a binary format following the header. TBJSON is useful for serializing known objects, classes, or types, otherwise it will offer little advantage if any in terms of size or performance over JSON. For a browser compatible version of this package, use TBJSON in the Browser. Each file starts off with .tbj to singinify that it is a Typed Binary JSON file, followed by a unit32 which is the length of the header.
      » npm install typed-binary-json
    
Published   Dec 03, 2024
Version   1.21.2
Author   Jeff Seaman
Find elsewhere
🌐
Medium
medium.com › @th30z › yet-another-json-binary-encoding-382d6e785273
Yet Another JSON Binary Encoding. YAJBE is a compact binary data format… | by Matteo Bertozzi | Medium
April 5, 2023 - Yet Another JSON Binary Encoding YAJBE is a compact binary data format built to be a drop-in replacement for JSON (JavaScript Object Notation). You can find the repository here …
🌐
npm
npmjs.com › search
binary json - npm search
Node Bindings for abieos: Binary <> JSON conversion using ABIs.
🌐
GitHub
github.com › JeffreyRiggle › bison
GitHub - JeffreyRiggle/bison: JSON to binary
A JSON to binary tool. This tool takes in JSON and converts it to a binary structure to save some bytes.
Starred by 8 users
Forked by 2 users
Languages   TypeScript 93.1% | JavaScript 6.9% | TypeScript 93.1% | JavaScript 6.9%
🌐
n8n
community.n8n.io › questions
Convert Binary file To JSON or CSV - Questions - n8n Community
March 28, 2023 - Describe the issue/error/question Hello everyone, I have to transform a file in Binary (more than 400mb) downloaded from sFTP server and then, use that data in a CSV or JSON format. Do you know the best way to handle a…
Top answer
1 of 1
1

There is a ".bin" file on my pc that i want to convert. I tried to do it with a ISO reader but it wont work. The only thing i know about this file, it stores some json data that i want to see.

Are you sure it's storing JSON-formatted data? If it were, then you'd be able to read it with a text editor. The whole idea of binary data is that the underlying structure is left out of what's written. Instead, you have to know which bytes correspond to which bits of data (the metadata/schema) when reading the binary data later. A program might know this schema and then translate it into JSON, but I suspect the file does not house JSON itself.

Note: I know it is a json file because a guy already parsed some info about this file but im not sure how he did it. He sent an image inside an text editor with this content:

file_name.bin_struct.txt


00-01 = example value 1
02-03 = example value 2
9B-0C = example value 3

Those look like addresses corresponding to bytes of data. First two bytes example 1, next two bytes example 2, skip many bytes to address 9B and then... rotate back to 0C? Maybe it's actually AC? In any case, there's more than two bytes for example 3.

This person you've referenced might be reverse-engineering the binary data, making educated guesses about which bytes are what. Or they might have the schema already and a program that uses the schema and spits out JSON from the bin file.

But regardless, if it's just binary data following an unknown metadata/schema, then no, there's no ready-made program to translate unknown binary data into a more meaningful format. At best, you could use something like the GNU/Linux file command to check if it happens to be a common binary format, or the strings command to peek around for human-readable data. After that, you've gotta break out the hex editor and start making educated guesses yourself. And before you know it, you can be a reverse engineer too :)

🌐
Stack Overflow
stackoverflow.com › questions › 63452203 › how-to-convert-binary-payload-to-json-key-value-pair-using-javascript
How to convert binary payload to JSON key-value pair using JavaScript? - Stack Overflow
August 17, 2020 - The scheme is the way that JavaScript should use to map the binary payload to the key-value JSON. The payload will have a version, so in case we changed the payload structure in new devices we just update the backend with the new one and we should be able to convert to either of them.
🌐
DataConverter
dataconverter.io › convert › xlsb-to-json
Convert Excel Binary to JSON Online - DataConverter.io
Excel Binary Workbook (.xlsb) is a Microsoft Excel file format that stores spreadsheet data in a binary format that makes it faster and more efficient for handling large datasets. It supports all Excel features. JSON (JavaScript Object Notation) is a lightweight, text-based format used for ...