class NestedMetadata {

  public static byte[] mergedPayload(byte[] payload, byte[] metadata) throws UncheckedIOException {
    // Ensure that we have a json object with no leading whitespace
    if (payload.length < 2 || payload[0] != '{') {
      throw new UncheckedIOException(new IOException("invalid json object: must start with {"));
    }

    // Create an output stream for joining metadata with payload
    final ByteArrayOutputStream payloadWithMetadata = new ByteArrayOutputStream(
        metadata.length + payload.length);
    // Write metadata without trailing `}`
    payloadWithMetadata.write(metadata, 0, metadata.length - 1);

    // Start next json field, unless object was empty
    if (payload.length > 2) {
      // Write comma to start the next field
      payloadWithMetadata.write(',');
    }

    // Write payload without leading `{`
    payloadWithMetadata.write(payload, 1, payload.length - 1);

    return payloadWithMetadata.toByteArray();
  }

  private  NestedMetadata();

}